stringByAppendingString question.

Member
Posts: 22
Joined: 2007.05
Post: #1
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;
}
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #2
gameNumberList = [NSMutableString alloc];

gameNumberList = [[NSMutableString alloc] init];
Quote this message in a reply
Member
Posts: 22
Joined: 2007.05
Post: #3
Caught that right after posting but I still do not get anything when trying:

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.
Quote this message in a reply
Member
Posts: 114
Joined: 2005.03
Post: #4
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.
Quote this message in a reply
Member
Posts: 22
Joined: 2007.05
Post: #5
Yep. appendString: seems to work. Course now I have an infinite loop to fix but getting a little closer. Thanks.
Quote this message in a reply
Post Reply