Original website: http://blog.csdn.net/itianyi/article/details/8661997#comments
SetValue Forkey and SetObject forkey are often used when using nsmutabledictionary, and they can often be used interactively, often in every use of the code.
The difference between the 2 of them is:
1, Setobject:forkey: In value is not able to nil, or will error.
Setvalue:forkey: Value can be nil, but when value is nil, the Removeobject:forkey method is called automatically
2, Setvalue:forkey: The parameter in key can only be nsstring type, and Setobject:forkey: can be any type.
Just as there is an advantage,SetValue value can be set to nil,setobject key can be any type .
Note: Setobject:forkey: objects cannot hold nil to differentiate from this situation:
1, [imagedictionarysetobject:[NSNullnull] forkey:indexnumber];
[NSNull null] Represents an empty object, not nil, note this
2, Setobject:forkey: When key is the NSNumber object, the following:
[imagedictionarysetobject:obj forkey:[nsnumber numberwithint:10];
The difference mentioned above is for the caller to be dictionary.
Setobject:forkey: Method Nsmutabledictionary Peculiar, and
Setvalue:forkey: Method is the Main method of KVC (key-value encoding).
When Setvalue:forkey: The method caller is an object:
Setvalue:forkey: The method is created in the NSObject object, which means that all OC objects have this method, so they can be used in any class.
For example, use:
SomeClass *someobj = [[SomeClass alloc] init];
[Someobj setvalue:self forkey:@ "Delegate"];
Object Someobj Set the value of his delegate property to the current class, and of course the object calling this method must have a delegate property to set, or the call will not be effective
Turn: The difference between SetValue and SetObject