Iphone 将NSMutableDictionary';s objectForKey:方法返回副本还是对象本身?

Iphone 将NSMutableDictionary';s objectForKey:方法返回副本还是对象本身?,iphone,ios,cocoa-touch,nsdictionary,Iphone,Ios,Cocoa Touch,Nsdictionary,NSMutableDictionary类的objectForKey:方法是否返回对象的副本 如果我在NSMutableDictionary中存储了一个NSMutableArray,并且我对使用字典的objectForKey:方法访问的数组进行了一些修改(比如添加一个对象),那么这些修改对存储在字典中的数组有效吗?否,它不返回副本 NSMutableArray *array = [NSMutableArray new]; NSMutableDictionary *dict = [NSMutable

NSMutableDictionary
类的
objectForKey:
方法是否返回对象的副本


如果我在
NSMutableDictionary
中存储了一个
NSMutableArray
,并且我对使用字典的
objectForKey:
方法访问的数组进行了一些修改(比如添加一个对象),那么这些修改对存储在字典中的数组有效吗?

否,它不返回副本

NSMutableArray *array = [NSMutableArray new];
NSMutableDictionary *dict = [NSMutableDictionary new];

NSLog(@"local instance of array: %p", array);

[dict setObject: array forKey: @"key"];

NSLog(@"returned from dictionary: %p", [dict objectForKey: @"key"]);
输出:

2012-09-16 14:06:36.879 Untitled 3[65591:707] local instance of array: 0x7f98f940a2f0
2012-09-16 14:06:36.884 Untitled 3[65591:707] returned from dictionary: 0x7f98f940a2f0
您将返回相同的指针,这意味着该对象尚未被复制。

[collection setObject:obj forKey:key];
您正在将该对象实例放入集合(字典、数组或集合)中

如果要在字典中添加对象的副本,必须这样做

[collection setObject:[obj copy] forKey:key];
无论如何,objectForKey:method返回的总是您输入的内容,而不是它的副本