ios NSMutableDictionary 中 setObject:forKey: 和 setValue:forKey: 的区别在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1249634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where's the difference between setObject:forKey: and setValue:forKey: in NSMutableDictionary?
提问by Oren Trutner
When looking at the documentation, I hardly see any big difference. Both "value" and "object" are of type id, so can be any object. Key is once a string, and in the other case an id. One of them seems to retain the object, and the other don't. What else? Which one is for what case?
查看文档时,我几乎看不出有什么大的不同。"value" 和 "object" 都是 id 类型,所以可以是任何对象。Key 曾经是一个字符串,在另一种情况下是一个 id。其中一个似乎保留了对象,而另一个则没有。还有什么?哪个是针对什么情况的?
回答by Oren Trutner
setValue:forKey:
is part of the NSKeyValueCodingprotocol, which among other things, lets you access object properties from the likes of Interface Builder. setValue:forKey:
is implemented in classes other than NSDictionary
.
setValue:forKey:
是NSKeyValueCoding协议的一部分,除其他外,它允许您从 Interface Builder 等访问对象属性。setValue:forKey:
在NSDictionary
.以外的类中实现。
setObject:forKey:
is NSMutableDictionary's
reason to exist. Its signature happens to be quite similar to setValue:forKey:, but is more generic (e.g. any key type). It's somewhat of a coincidence that the signatures are so similar.
setObject:forKey:
是NSMutableDictionary's
存在的理由。它的签名恰好与 setValue:forKey: 非常相似,但更通用(例如任何键类型)。签名如此相似有点巧合。
What adds to the confusion is that NSMutableDictionary's implementation of setValue:forKey:
is equivalent to setObject:forKey:
in most cases. In other classes, setValue:forKey:
changes member variables. In NSMutableDictionary
, it changes dictionary entries, unless you prefix the key with a '@' character -- in which case it modifies member variables.
更令人困惑的是,在大多数情况下,NSMutableDictionary 的实现setValue:forKey:
等效setObject:forKey:
于。在其他类中,setValue:forKey:
更改成员变量。在 中NSMutableDictionary
,它会更改字典条目,除非您在键前添加一个“@”字符——在这种情况下,它会修改成员变量。
So, in a nutshell, use setObject:forKey:
when you need to work with dictionary keys and values, and setValue:forKey:
in the rarer cases where you need to tackle KVP.
因此,简而言之,setObject:forKey:
当您需要使用字典键和值时使用,以及setValue:forKey:
在需要处理KVP的极少数情况下使用。
EDIT: and oh, it looks like this has been asked and answered before: Difference between objectForKey and valueForKey?
编辑:哦,看起来之前已经问过并回答过:objectForKey 和 valueForKey 之间的区别?
回答by user102008
Another difference is that if you give a nil value to setValue:forKey:
, it removes the key from the dictionary if it exists, otherwise does nothing. But if you give a nil value to setObject:forKey:
, it raises an exception.
另一个区别是,如果给 一个 nil 值setValue:forKey:
,它会从字典中删除键(如果存在),否则什么都不做。但是如果你给 一个 nil 值setObject:forKey:
,它会引发一个异常。
回答by Jakub Truhlá?
-setValue:forKey:
just send -setObject:forKey:
to the receiver, unless the value is nil
, in which case send -removeObjectForKey
.
-setValue:forKey:
只发送-setObject:forKey:
给接收者,除非值是nil
,在这种情况下发送-removeObjectForKey
。
Dead simple.
死的简单。
回答by breakfreehg
anObject— The value for key. The object receives a retain message before being added to the NSDictionary
. This value must not be nil.
anObject— 键的值。对象在被添加到NSDictionary
. 该值不能为零。
aKey— The key for value. The key is copied (using copyWithZone:
; keys must conform to the NSCopying
protocol). The key must not be nil.
aKey— 值的键。密钥被复制(使用copyWithZone:
; 密钥必须符合NSCopying
协议)。键不能为零。
value— The value for key.
value— 键的值。
key— The key for value. Note that when using key-value coding, the key must be a string (see “Key-Value Coding Fundamentals”).
key— 值的键。请注意,使用键值编码时,键必须是字符串(参见“键值编码基础”)。