附加字符串不起作用

时间:2012-10-29 00:49:27

标签: ios nsstring

我正在尝试这段代码......

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

来自该对象的5个char字符串是“t0001”,所以我想最终得到“t0001000”但它不起作用,当我检查sharedInstance.groundMap时,数组中的所有字符串仍为“t0001”< / p>

我应该使用NSMutableString吗?

1 个答案:

答案 0 :(得分:3)

您没有分配结果。改变这个:

[tileString stringByAppendingString:@"0000"];

为:

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

你可以做更多的清理工作:

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