iOS Learning Note (6) Key-value code--KVC

Source: Internet
Author: User

In KVC programming, regardless of whether you call the Setvalue:forkey: method or the Valueforkey: method, you specify the manipulated property by the NSString object, where Forkey: the label user passes in the property name.

For setValue: attribute value [email protected] "name"; code, the underlying execution mechanism is as follows.

(1) Program priority call "SetName: attribute value;" The code is set up through the setter method.

(2) If the class does not have a SetName: method, the KVC mechanism searches for a member variable named _name, regardless of whether the member variable is defined in the Class Interface section, or in the class implementation section, and regardless of the access control modifier. The top layer of this KVC code is actually assigning values to _name member variables.

(3) If the class has neither the SetName: method nor the _NAME member variable defined, the KVC mechanism searches for a member variable of the class named name, whether the member variable is defined in the Class Interface section, or in the class implementation section, and regardless of the access control modifier. The top layer of the KVC code is actually assigning a value to the name member variable.

(4) If none of the above 3 are found, the system will execute the object's Setvalue:forundefinedkey: method.

For Valueforkey: @name; Code, the underlying execution mechanism is as follows.

(1) The procedure gives priority to calling "name;" Code to get the return value of the Getter method.

(2) If the class does not have a name method, the KVC mechanism searches for a member variable with the class named _name, whether the member variable is defined in the Class Interface section, or in the class implementation section, and regardless of the access control modifier, the top layer of the KVC code is actually a return _ The value of the name member variable.

(3) If the class has neither the name method nor the _NAME member variable defined, the KVC mechanism searches for a member variable of the class named name, whether the member variable is defined in the Class Interface section, or in the class implementation section, and regardless of the access control modifier. The top layer of the KVC code is actually the value that returns the name member variable.

(4) If none of the above 3 are found, the system will execute the object's Valueforundefinedkey: method.

  Tips:

(1) Default Setvalue:forundefinedkey: method and Valueforundefinedkey: The method implementation is to throw an exception that will cause the program to end unexpectedly. If you do not want to end the program because there is no key, you can override both methods.

(2) When the program attempts to set a nil value for a property, if the property does not accept the nil value, the program will automatically execute the object's Setnilvalueforkey: method. If you want to make this behavior yourself, you can do so by overriding the Setnilvalueforkey: method.

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.