NSArrayの値を更新する方法のメモ

(XcodeのiPhone Simulator 6.0で試しています。)

ポイント

・インスタンスを変更する

NSArrayは追加、削除等が出来ない、値の更新を行う場合、

インスタンスを再度代入するという方法があります。

サンプルコード

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    // テスト用の配列を用意する

    NSArray *array = [[NSArray alloc] initWithObjects:@”one”, @”two”, @”three”, nil];

    

    // 出力

    for (NSString *word in array) {

        NSLog(@”old value:%@”, word);

    }

    

    // 配列を更新する

    array = [[NSArray alloc] initWithObjects:@”four”, @”five”, @”six”, nil];

    // 出力

    for (NSString *word in array) {

        NSLog(@”old value:%@”, word);

    }

}

@end