Objective-Cで文字列を結合する方法のメモ

ポイント

・NSStringのメソッドを使う

サンプル

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    // パターンを準備

    NSString *strA = @”This is A.”;

    NSString *strB = @”This is B.”;

    

    //方法1 stringByAppendingStringで結合

    NSString *strAB = [strA stringByAppendingString: strB];

    NSLog(@”%@”, strAB);

    

    

    //方法2 formatで結合

    // NSLog(@”%@%@”, strA, strB);

    strAB = [NSString stringWithFormat:@”%@%@”, strA, strB];

    NSLog(@”%@”, strAB);

}

@end