LoginSignup
5
4

More than 5 years have passed since last update.

NSMutableDictionaryの階層化をMutableCopyして変更する時の注意点

Last updated at Posted at 2014-07-17

ダメ

NSMutableDictionary * dictionary = [NSMutableDictionary dictionaryWithObject:[NSMutableArray arrayWithObjects:@"上田",@"山田",@"川田", nil] forKey:@"name"];

NSMutableDictionary *copydictonary = [dictionary MutableCopy];

[[dictionary objectForKey:@"name"] removeObjectAtIndex:0];

dictionary = [copydictonary MutableCopy];

上記の場合、dictionaryの変更がcopydictonaryにも適用されてしまう様だ。

Ok

NSMutableDictionary * dictionary = [NSMutableDictionary dictionaryWithObject:[NSMutableArray arrayWithObjects:@"上田",@"山田",@"川田", nil] forKey:@"name"];
NSMutableArray *copy = [[dictionary objectForKey:@"name"] mutableCopy];

[[dictionary objectForKey:@"name"] removeObjectAtIndex:0];

[dictionary setObject:copy forKey:@"name"];

変更する階層まで潜って、取り出さないと元を変更すると上書きしてしまうようでした。

5
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4