SetValue Forkey and SetObject forkey are often used when using nsmutabledictionary, and they are often used interactively, often in every use of the code.
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
Expand a category of Nsmutabledictionary, which is clearly stated above, sending Setobject:forkey to the receiver, which means calling the Setobject:forkey method
The Removeobejct:forkey method is invoked automatically when the method Removeobject:forkey (value is nil) unless value is nil
Setobject:forkey:
@interface Nsmutabledictionary:nsdictionary
-(void) Removeobjectforkey: (ID) Akey;
-(void) SetObject: (ID) anobject forkey: (id <NSCopying>) Akey;
@end
Note: Setobject:forkey: The object of the key is an ID type, not nsstring, but we often use nsstring.
The difference between SetValue and SetObject is:
1, Setobject:forkey: The value can not be nil, or it will be an error.
Setvalue:forkey: The value can be nil, but when value is nil, the Removeobject:forkey method is automatically invoked
2, Setvalue:forkey: The parameters of the key can only be nsstring type, and Setobject:forkey: can be any type
Note: Setobject:forkey: objects cannot be stored nil to be differentiated from the following:
1, [Imagedictionarysetobject:[nsnullnull] forkey:indexnumber];
[Nsnull null] represents an empty object, not nil, notice this
2, Setobject:forkey: The key is the NSNumber object, as follows:
[Imagedictionarysetobject:obj Forkey:[nsnumber Numberwithint:10]];
Attention:
The difference mentioned above is for the caller to be dictionary.
Setobject:forkey: Method Nsmutabledictionary Peculiar, while
Setvalue:forkey: Method is the primary 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 with any class.
For example, use:
SomeClass *someobj = [[SomeClass alloc] init];
[Someobj 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 the delegate property to set it, otherwise the call will not be effective