NSMutableDictionary
类的objectForKey:
方法是否返回对象的副本?
如果我有一个 NSMutableArray
存储在 NSMutableDictionary
中,并且我对使用 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
您将返回相同的指针,这意味着该对象尚未被复制。
关于iphone - 请问NSMutableDictionary的objectForKey : method return copy or the object itself?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12447070/