IOS.KVC.setValue:forKey:

Source: Internet
Author: User

The Foundation Framework defines NSObject (nskeyvaluecoding),

-(void) SetValue: (ID) value Forkey: (NSString *) key;

Description of the interface

1. Search for method-set<key> in the class of message receiver first:

2. If not found in 1, search for _<key> in class, _is<key>, <key>, is<key> instance variable

3. If not found in 2, call method-setvalue:forundefinedkey:, and the default implementation of the method is to throw

Exception nsundefinedkeyexception

The next question, then, in the iOS SDK, so many frameworks, so many classes, then which class covers the NSObject

What about the default implementation of-setvalue:forundefinedkey:?

1 IMP animationimp = [caanimation instancemethodforselector: @selector (setvalue:forundefinedkey:)]; 2 3 IMP defaultimp = [nsobject instancemethodforselector: @selector (setvalue:forundefinedkey:)]; 4 5 IMP viewimp = [UIView instancemethodforselector: @selector (setvalue:forundefinedkey:)]; 6 7 NSLog (@ "defaultimp:%p, Animationimp:%p, Viewimp:%p", Defaultimp, Animationimp, Viewimp);

The output of log is: defaultimp:0x125dc84, ANIMATIONIMP:0X10D5CA, viewimp:0x125dc84

Visible UIView does not overwrite the default implementation of NSObject, which is the same class and NSObject classes that match the return values of the following calls


So when using-setvalue:forkey: You need to use Try-catch to wrap the call to this method.

IOS.KVC.setValue:forKey:

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.