iDevGames Forums
stringByAppendingString question. - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: stringByAppendingString question. (/thread-3204.html)



stringByAppendingString question. - hypnotx - Jun 29, 2007 02:15 PM

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;
}



stringByAppendingString question. - LongJumper - Jun 29, 2007 03:50 PM

gameNumberList = [NSMutableString alloc];

gameNumberList = [[NSMutableString alloc] init];


stringByAppendingString question. - hypnotx - Jun 29, 2007 04:08 PM

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.


stringByAppendingString question. - Cochrane - Jun 30, 2007 03:13 AM

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.


stringByAppendingString question. - hypnotx - Jul 3, 2007 11:29 AM

Yep. appendString: seems to work. Course now I have an infinite loop to fix but getting a little closer. Thanks.