Phil
Phil

Reputation: 3045

Appending string is not working

I'm trying this code...

NSString *tileString = [[NSString alloc] init];    

for (int i = 0; i < [[GameP objectForKey:@"groundMap"] length]; i += 5) {

            tileString = [[GameP objectForKey:@"groundMap"] substringWithRange:NSMakeRange(i, 5)];

            [tileString stringByAppendingString:@"0000"];

            [sharedInstance.groundMap addObject:tileString];
        }

The 5 char string that's coming from the object is "t0001", so I want to end up with "t0001000" but it's not working, when I check sharedInstance.groundMap all the strings in the array are still "t0001"

Should I be using NSMutableString instead?

Upvotes: 0

Views: 57

Answers (1)

rmaddy
rmaddy

Reputation: 318955

You aren't assigning the result. Change this:

[tileString stringByAppendingString:@"0000"];

to:

titleString = [tileString stringByAppendingString:@"0000"];

There's a little more cleanup you can do too:

for (int i = 0; i < [[GameP objectForKey:@"groundMap"] length]; i += 5) {
    NSString *tileString = [[GameP objectForKey:@"groundMap"] substringWithRange:NSMakeRange(i, 5)];
    titleString = [tileString stringByAppendingString:@"0000"];
    [sharedInstance.groundMap addObject:tileString];
}

Upvotes: 3

Related Questions