我有一个函数
-(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"
我怎么做 ?
有很多方法可以做到这一点。在这种情况下最简单的是:
myString = [NSString stringWithFormat:@"level + %d", currentLevel]
Or
myString = [NSString stringWithFormat:@"%@ %d", initialString, currentLevel]
您还可以考虑反复使用 valueForKeyPath: 而不是 objectForKey。 ([x valueForKeyPath:@"a.b.c"]
类似于[[[x objectForKey:@"a"] objectForKey:@"b"] objectForKey:@"c"]
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)