我有一个功能
-(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"
我该怎么做?
答案 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"]
)