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.
1. First look at setValue: forKey:
the definition
Extension Nsmutabledictionary A category, the above comment is very clear, send setobject:forkey to the receiver, that is, call Setobject:forkey method, unless value is nil, Call Method Removeobject:forkey
2. Look at setObject:forKey:
the definition
Note: The setObject:forKey:
object in key is an ID type, not a nsstring, but we often use nsstring.
3. Summarize the differences between the two:
- Setobject:forked: The object is not nil, or it will be an error.
Setvalue:forkey: Value can be nil, but when value is nil, the Removeobject:forkey method is called automatically
- Setvalue:forkey: The parameter in key can only be nsstring type, and Setobject:forkey: can be any type
Note:Setobject:forkey: Objects cannot hold nil to differentiate from this situation:
[imageDictionary setObject:[NSNullnull] forKey:indexNumber];
[NSNull null] represents an empty object, not nil, note this
setObject:forKey:
When key is the NSNumber object, the following:
[imageDictionary setObject:obj forKey:[NSNumber numberWithInt:10]];
- The difference mentioned above is for the caller to be dictionary.
setObject:forKey:
method Nsmutabledictionary is unique, and the setValue:forKey:
method is the main method of KVC (key-value coding).
- When the
setValue:forKey:
method caller is an object: The setValue:forKey:
method is created in the NSObject object, which means that all OC objects have this method, so they can be used in any class.
4. Use:
setValue:self forKey:@"delegate"];
Object Someobj Sets the value of his delegate property to the current class, and of course the object calling this method must have a delegate property in order to be set, otherwise the call has no effect.
Original address: http://blog.csdn.net/itianyi/article/details/8661997
Links: http://www.jianshu.com/p/8c9d31333b34
The difference between IOS SetValue and SetObject