在Objective-C中是否存在( stringByAppendingString:
字符串链接的快捷方式,或者一般使用NSString
快捷方式? 数组
例如,我想作: url
NSString *myString = @"This"; NSString *test = [myString stringByAppendingString:@" is just a test"];
更像是: spa
string myString = "This"; string test = myString + " is just a test";
NSString *label1 = @"Process Name: "; NSString *label2 = @"Process Id: "; NSString *processName = [[NSProcessInfo processInfo] processName]; NSString *processID = [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]]; NSString *testConcat = [NSString stringWithFormat:@"%@ %@ %@ %@", label1, processName, label2, processID];
如何缩短stringByAppendingString
并使用#define : code
#define and stringByAppendingString
所以你会使用: component
NSString* myString = [@"Hello " and @"world"];
问题是它只适用于两个字符串,你须要包含额外的括号以得到更多的附加内容: orm
NSString* myString = [[@"Hello" and: @" world"] and: @" again"];
建立AppendString(AS)宏的快捷方式......
排序
#define AS(A,B) [(A) stringByAppendingString:(B)]
NSString *myString = @"This"; NSString *test = AS(myString,@" is just a test");
注意: three
若是使用宏,固然只需使用可变参数,请参阅EthanB的答案。 字符串
我一直回到这篇文章,并老是最终对答案进行排序,找到这个简单的解决方案,根据须要使用尽量多的变量: string
[NSString stringWithFormat:@"%@/%@/%@", three, two, one];
例如:
NSString *urlForHttpGet = [NSString stringWithFormat:@"http://example.com/login/username/%@/userid/%i", userName, userId];
这是一种简单的方法,使用新的数组文字语法:
NSString * s = [@[@"one ", @"two ", @"three"] componentsJoinedByString:@""]; ^^^^^^^ create array ^^^^^ ^^^^^^^ concatenate ^^^^^