Where's the difference between setObject:forKey: and setValue:forKey: in NSMutableDictionary?

IosIphoneNsmutabledictionary

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

setValue:forKey: is part of the NSKeyValueCoding protocol, which among other things, lets you access object properties from the likes of Interface Builder. setValue:forKey: is implemented in classes other than 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.

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.

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.

EDIT: and oh, it looks like this has been asked and answered before: https://stackoverflow.com/questions/1062183/objective-c-whats-the-difference-between-objectforkey-and-valueforkey

Solution 2 - Ios

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.

Solution 3 - Ios

-setValue:forKey: just send -setObject:forKey: to the receiver, unless the value is nil, in which case send -removeObjectForKey.

Dead simple.

Solution 4 - Ios

anObject — The value for key. The object receives a retain message before being added to the NSDictionary. This value must not be nil.

aKey — The key for value. The key is copied (using copyWithZone:; keys must conform to the NSCopying protocol). The key must not be nil.

value — The value for key.

key — The key for value. Note that when using key-value coding, the key must be a string (see “Key-Value Coding Fundamentals”).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHelloMoonView Question on Stackoverflow
Solution 1 - IosOren TrutnerView Answer on Stackoverflow
Solution 2 - Iosuser102008View Answer on Stackoverflow
Solution 3 - IosJakub TruhlářView Answer on Stackoverflow
Solution 4 - IosbreakfreehgView Answer on Stackoverflow