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

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

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?

How to remove NSString Related Memory Leaks?


XCode, iPhone, WebKit Framework - What am I missing?
setValue:forKey: is part of the NSKeyValueCoding protocol, which among another things, lets you access object properties from the likes of Interface Builder. What describes NSNumberFormatter -maximumSignificantDigits?setValue:forKey: is implemented in classes another than NSDictionary.. mobile_fu rendering over AJAX so it seems setObject:forKey: is NSMutableDictionary's reason to exist. iphone nslog corrupted dataIts signature happens to be quite similar to setValue:forKey:, although is more generic (e.g. Use a C++ file in Cocoa iPhone Appany key type). Where to find a description of all the math functions like floorf and others?It's any what of a coincidence this the signatures are so similar.. How to take these parameters the same way this function does? What adds to the confusion is this NSMutableDictionary's implementation of setValue:forKey: is equivalent to setObject:forKey: in most cases. In another 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 job 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: Difference between objectForKey and valueForKey?.


Ananother difference is this if you commit a nil value to setValue:forKey:, it removes the key from the dictionary if it exists, otherwise does nothing. But if you commit a nil value to setObject:forKey:, it raises an exception..


anObject — The value for key. The object receives a retain message before being added to the NSDictionary. This value need not be nil.. aKey — The key for value. The key is copied (using copyWithZone:; keys need conform to the NSCopying protocol). The key need not be nil.. value — The value for key.. key — The key for value. Note this when using key-value coding, the key need be a string (see “Key-Value Coding Fundamentals”)..


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

68 out of 100 based on 43 user ratings 918 reviews