Shortcuts in Objective-C to concatenate NSStrings

前端 未结 30 2605
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 07:03

Are there any shortcuts to (stringByAppendingString:) string concatenation in Objective-C, or shortcuts for working with NSString in general?

30条回答
  •  一向
    一向 (楼主)
    2020-11-22 08:05

    Either of these formats work in XCode7 when I tested:

    NSString *sTest1 = {@"This" " and that" " and one more"};
    NSString *sTest2 = {
      @"This"
      " and that"
      " and one more"
    };
    
    NSLog(@"\n%@\n\n%@",sTest1,sTest2);
    

    For some reason, you only need the @ operator character on the first string of the mix.

    However, it doesn't work with variable insertion. For that, you can use this extremely simple solution with the exception of using a macro on "cat" instead of "and".

提交回复
热议问题