Arrayを使う際の、MutableとImmutableの変換方法のメモ

ポイント

・copy

サンプルコード

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    NSMutableArray *mutable = [[NSMutableArray alloc] initWithObjects:@”test”, @”test2″, nil];

    

    

    // Mutable -> Immutable

    NSArray *newArr = [mutable copy];

    

    

    // 出力

    NSLog(@”%@”, newArr);

    

 

    

    // 参考

    // copyするとMutableではなくなるので、こうするとエラーになる。

    // Error: __NSArrayI addObject:

    // NSMutableArray *newArr = [mutable copy];

    // [newArr addObject:@”test3″];

}

@end