2011-12-04 163 views
0

我有一个函数如何将字符串/数字附加到字符串?

-(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为字符串“水平”添加为objectForKey:@"level + STRING/NUMBER"

我该怎么办呢?

回答

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"]

+0

太棒了!非常感谢你!顺便说一下,我真正想要的是'myString = [NSString stringWithFormat:@“level%d”,currentLevel]'没有加号... :) –