【问题标题】:How do I append a string/number to a string ?如何将字符串/数字附加到字符串?
【发布时间】:2012-01-12 09:58:27
【问题描述】:

我有一个功能

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

我该怎么做?

【问题讨论】:

    标签: objective-c string append


    【解决方案1】:

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

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

    【讨论】:

    • 太棒了!!!非常感谢!顺便说一句,我真正想要的是myString = [NSString stringWithFormat:@"level%d", currentLevel] 没有加号... :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2012-10-04
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多