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.