<span id="Label3"></p><p><p>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.</p></p><p><p>1, First look at the definition of setvalue:forkey:</p></p><p><p></p></p><p><p>@interface Nsmutabledictionary (nskeyvaluecoding)</p></p><p><p>/* send-setobject:forkey:to the receiver, unless the value is nil, in which case send-removeobject:forkey:.</p></p><p><p>*/</p></p><p><p>-(void) setValue: (id) value forkey: (nsstring *) key;</p></p><p><p>@end</p></p><p><p>Extension Nsmutabledictionary A category, the above comment is very clear, send Setobject:forkey to the receiver, that is, call Setobject:forkey method</p></p><p><p>Unless value is nil, the method is called Removeobject:forkey</p></p><p><p></p></p><p><p>2, look at the definition of setobject:forkey:</p></p><p><p></p></p><p><p>@interface Nsmutabledictionary:nsdictionary</p></p><p><p>-(void) removeobjectforkey: (id) akey;</p></p><p><p>-(void) setobject: (id) anobject forkey: (id <NSCopying>) akey;</p></p><p><p>@end</p></p><p><p><span style="color: #ff0000;">Note: setobject:forkey: The object in key is an ID type, not a nsstring, but we often use nsstring.</span></p></p><p><p></p></p><p><p>Now the difference between the 2 of them is:</p></p><p><p>1, setobject:forkey: in value is not able to nil, or will Error.</p></p><p><p>Setvalue:forkey: value can be nil, but when value is nil, the Removeobject:forkey method is called automatically</p></p><p><p>2, setvalue:forkey: The parameter in key can only be nsstring type, and setobject:forkey: can be any type</p></p><p><p></p></p><p><p><span style="color: #ff0000;">Note: Setobject:forkey: objects cannot hold nil to differentiate from this situation:</span></p></p><p><p></p></p><p><p><span style="color: #ff0000;">1, [imagedictionarysetobject:[nsnullnull] forkey:indexnumber];</span></p></p><p><p><span style="color: #ff0000;">[NSNull null] represents an empty object, not nil, Note this</span></p></p><span style="color: #ff0000;"><span style="color: #ff0000;"> </span></span><p><p><span style="color: #ff0000;"></span></p></p><p><p><span style="color: #ff0000;">2, setobject:forkey: When key is the NSNumber object, the Following:</span></p></p><p><p><span style="color: #ff0000;">[imagedictionarysetobject:obj Forkey:[nsnumber numberwithint:10];</span></p></p><p><p></p></p><p><p><span style="color: #ff0000;">Attention:</span></p></p><p><p><span style="color: #ff0000;">The difference mentioned above is for the caller to be dictionary.</span></p></p><p><p><span style="color: #ff0000;">Setobject:forkey: method Nsmutabledictionary peculiar, and</span></p></p><p><p><span style="color: #ff0000;">Setvalue:forkey: method is the main method of KVC (key-value encoding).</span></p></p><p><p><span style="color: #ff0000;"></span></p></p><p><p><span style="color: #888888;">When setvalue:forkey: the method caller is an object:</span></p></p><p><p><span style="color: #888888;">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.</span></p></p><p><p><span style="color: #888888;">For example, use:</span></p></p><p><p><span style="color: #888888;">SomeClass *someobj = [[someclass alloc] init];</span></p></p><p><p><span style="color: #888888;">[someobj setvalue:self forkey:@ "delegate"];</span></p></p><p><p><span style="color: #888888;">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</span></p></p><p><p><span style="color: #ff0000;"></span></p></p><p><p>IOS [switch]setvalue and SetObject differences</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service