如何将字符串/数字附加到字符串?

时间:2011-12-04 00:10:48

标签: objective-c string append

我有一个功能

 -(void) generateLevelFromPlist:(int)currentLevel{
    NSString *mainPath = [[NSBundle mainBundle] bundlePath];
    itemPositionPlistLocation = [mainPath stringByAppendingPathComponent:@"levelconfig.plist"];
    NSDictionary * itemPositions = [[NSDictionary alloc] initWithContentsOfFile:itemPositionPlistLocation];
    NSNumber *xVal = [[[[itemPositions objectForKey:@"level + STRING/NUMBER"]objectForKey:@"hexposition"]objectForKey:@"hexagon1"]objectForKey:@"xVal"];
    int generatedXVal = [xVal integerValue];
    NSLog(@"%d", generatedXVal);
    NSLog(@"%@", itemPositionPlistLocation);



    }

我希望将变量currentLevel添加到字符串“level”,如objectForKey:@"level + STRING/NUMBER"

我该怎么做?

1 个答案:

答案 0 :(得分:7)

有很多方法可以做到这一点。在这种情况下最简单的是:

myString = [NSString stringWithFormat:@"level + %d", currentLevel]

或者

myString = [NSString stringWithFormat:@"%@ %d", initialString, currentLevel]

你也可以考虑反复使用valueForKeyPath:而不是objectForKey。 ([x valueForKeyPath:@"a.b.c"]类似于[[[x objectForKey:@"a"] objectForKey:@"b"] objectForKey:@"c"]