SetValue Forkey and SetObject forkey are often used when using nsmutabledictionary, and they can often be used interactively.
Definition of 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
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, the method is called Removeobject:forkey
Definition of 2.setobject:forkey
@interface nsmutabledictionary:nsdictionary
-(void) Removeobjectforkey: (id) akey;
-(void) SetObject: (ID) anobject Forkey: (ID <nscopying>) Akey;
@end
Note: The object of key in Setobject:forkey is an ID type, not a nsstring, but we often use nsstring.
The difference between the two:
1, Setobject:forkey value is not able to nil, or will error
The value in Setvalue:forkey can be null, but when value is empty, the Removeobject:forkey method is called automatically
2, setvakue:forkey key parameter can only be nsstring type, and Setobjecct:forkey can be any type
Attention:
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
The difference between SetValue and setobject