本文介绍了如何将字符串/数字附加到字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能

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

where I want to add the variable currentLevel to the string "level" as in objectForKey:@"level + STRING/NUMBER"

我该怎么做?

推荐答案

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

There are lots of ways to do this. The easiest in this case is:

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

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

您也可以考虑反复使用 valueForKeyPath: 而不是 objectForKey.([x valueForKeyPath:@"abc"] 类似于 [[[x objectForKey:@"a"] objectForKey:@"b"] objectForKey:@"c"]代码>)

You might also consider using valueForKeyPath: rather than objectForKey over and over. ([x valueForKeyPath:@"a.b.c"] is similar to [[[x objectForKey:@"a"] objectForKey:@"b"] objectForKey:@"c"])

这篇关于如何将字符串/数字附加到字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:53