[英]Does NSMutableDictionary keep a strong reference to an object I put in it via 'setValue:forKey:'?
According to Apple's documentation, if I add an object to an NSMutableDictionary
via the setObject:forKey:
method, the dictionary will keep a strong reference to it. 根据Apple的文档,如果我通过setObject:forKey:
方法将对象添加到NSMutableDictionary
,则字典将对其进行setObject:forKey:
引用。
But what about the setValue:forKey:
method? 但是setValue:forKey:
方法呢? The documentation doesn't say anything about the kind of reference to the object added via this method. 文档没有说明通过这种方法添加的对象的引用种类。 Is it the same as the other one? 与另一个相同吗? Does the dictionary keep a strong reference to the object added via this method as well? 字典是否也强烈引用通过此方法添加的对象?
NSMutableDictionary
(和NSMutableArray
)保持对添加到其中的任何对象的强引用,无论它如何添加。
NSMutableArray
and NSArray
keep strong references to objects they contain. NSMutableArray
和NSArray
对其包含的对象保持强引用。 These objects are either added using the literal constructor @[myObject, myObject2, ...]
, the initializers of NSArray
, or the add/insert methods of NSMutableArray
. 这些对象可以使用文字构造函数@[myObject, myObject2, ...]
, NSArray
的初始化程序或NSMutableArray
的add / insert方法添加。
If you're looking for key/value storage like a map, you want to use NSDictionary
/ NSMutableDictionary
instead. 如果要查找像地图一样的键/值存储, NSDictionary
改用NSDictionary
/ NSMutableDictionary
。 The setValue:forKey:
method you're talking about is for key-value coding compliance, which is different. 您正在谈论的setValue:forKey:
方法是针对键值编码合规性的,这是不同的。
Additionally, NSDictionary
and NSMutableDictionary
keep strong references to value objects, and keep copy references to keys (which must comply to the NSCopying
protocol). 此外, NSDictionary
和NSMutableDictionary
保留对值对象的强引用,并保留对键的副本引用(必须遵守NSCopying
协议)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.