在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 ^^^^^