stringByAppendingString question.
What am I doing wrong here? The code I am testing compiles and runs but I end up with an empty string for gameNumberList. The line in question is:
Code:
[gameNumberList stringByAppendingString:gameNumber];Code:
- (NSMutableString *)parseGameNumbers {
// let's parse the game numbers from the History File and pass them back to the caller as a string.
NSScanner *theScanner;
NSString *GAMENUMBERID = @"Full Tilt Poker Game #";
NSCharacterSet *colonSet;
NSError *error;
NSString *stringFromFileAtPath = [[NSString alloc] initWithContentsOfFile:historyFile encoding:NSUTF8StringEncoding error:&error];
NSMutableString *gameNumber;
NSMutableString *gameNumberList;
gameNumberList = [NSMutableString alloc];
colonSet = [NSCharacterSet characterSetWithCharactersInString:@":"];
theScanner = [NSScanner scannerWithString:stringFromFileAtPath];
if (stringFromFileAtPath == nil)
{
// an error occurred
NSLog(@"Error reading file at %@\n%@", historyFile, [error localizedFailureReason]);
}
while ([theScanner isAtEnd] == NO)
{
if ([theScanner scanString:GAMENUMBERID intoString:NULL] && [theScanner scanUpToCharactersFromSet:colonSet intoString:&gameNumber])
{
// we are not getting the game numbers to append here.
[gameNumberList stringByAppendingString:gameNumber];
return gameNumberList;
}
// else return NO;
}
return gameNumberList;
}
gameNumberList = [NSMutableString alloc];
gameNumberList = [[NSMutableString alloc] init];
gameNumberList = [[NSMutableString alloc] init];
Caught that right after posting but I still do not get anything when trying:
Watching it in debug and everything is fine just nothing goes into gameNumberList. I am wondering if I should be appending the string differently.
Code:
[gameNumberList stringByAppendingString:gameNumber];Watching it in debug and everything is fine just nothing goes into gameNumberList. I am wondering if I should be appending the string differently.
I'm willing to bet that stringByAppendingString is working perfectly here, it's just not what you want to use in the first place. stringByAppendingString returns a new string, but it doesn't change the target. You want appendString: instead.
Yep. appendString: seems to work. Course now I have an infinite loop to fix but getting a little closer. Thanks.

