In the Nsmutabledictionary method there are SetValue Forkey and SetObject Forkey, which can be used to set a value corresponding to a key.
1,setvalue:forkey:
@interface Nsmutabledictionary (nskeyvaluecoding)
/* Send-setobject:forkey:to the receiver, unless the value is nil, in which case Send-removeobject:forkey:.
*/
-(void) SetValue: (ID) value Forkey: (NSString *) key;
@end
Send Setobject:forkey to the receiver, that is, call the Setobject:forkey method, unless value is nil, call the method Removeobject:forkey
2,setobject:forkey:
@interface Nsmutabledictionary:nsdictionary
-(void) Removeobjectforkey: (ID) Akey;
-(void) SetObject: (ID) anobject forkey: (id <NSCopying>) Akey;
@end
Note: Setobject:forkey: The object in key is an ID type, not a nsstring, but we often use nsstring.
The difference between the two:
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
3, the two mentioned above are for the caller is dictionary. Because: Setobject:forkey: Method Nsmutabledictionary Unique, and Setvalue:forkey: Method is the Main method of KVC (key-value encoding). When Setvalue:forkey: The 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.
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 between SetValue and SetObject