The difference between IOS SetValue and SetObject

Source: Internet
Author: User

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:

  1. 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
  2. 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:

    1. [imageDictionary setObject:[NSNullnull] forKey:indexNumber];
      [NSNull null] represents an empty object, not nil, note this
    2. setObject:forKey:When key is the NSNumber object, the following:
      [imageDictionary setObject:obj forKey:[NSNumber numberWithInt:10]];
    3. 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).
    4. 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

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.