使用变量附加文本目标C.

时间:2013-05-09 23:07:09

标签: objective-c string nsstring append

我尝试附加文本但没有成功,直到我尝试了这个:

NSString *n_result = @"";
n_result = [n_result stringByAppendingString:[NSString stringWithFormat:@"The number "]];
    n_result = [n_result stringByAppendingString:[NSString stringWithFormat:@"%@ ", n_analyze]];
    n_result = [n_result stringByAppendingString:[NSString stringWithFormat:@", has "]];
    n_result = [n_result stringByAppendingString:[NSString stringWithFormat:@"%i ", steps]];
    n_result = [n_result stringByAppendingString:[NSString stringWithFormat:@"steps to reach 1"]];

问题是,必须有一种更简单的方法。但我不知道怎么做。 谁能帮我这个?我一直在寻找如何做到这一点“更好”。

2 个答案:

答案 0 :(得分:3)

[NSString stringWithFormat:@"The number %@ , has %i steps to reach 1", n_analyze, steps]

答案 1 :(得分:3)

是的,有一种稍微简单的方法。使用NSMutableString

的实例
NSMutableString *mutableString = [NSMutableString stringWithFormat:@"The number "];
[mutableString appendFormat:@"%@ ", n_analyze];

// etc..