—————————————————————————————————————————————————————————————
The basic concept of a KVC
KVC is the abbreviation of key value coding, which means the key values are encoded.
In iOS, there is a way to indirectly access an object's properties by using the name of the property (that is, key), which can access the properties of the object without using the Getter/setter method.
The mechanism by which KVC can indirectly access object properties. Often we use Valueforkey instead of getter methods, Setvalue:forkey to replace setter methods.
—————————————————————————————————————————————————————————————
Two common methods and characteristics
1) Common methods of KVC
-(ID) Valueforkey: (NSString *) key; ------Read the properties of an object based on the value of key
-(void) SetValue: (ID) value Forkey: (NSString *) key; ------Write the properties of the object based on the value of the key
Setnilvalueforkey: Called when Nil is set on a non-class object property, the exception is thrown by default.
Valueforundefinedkey:------Error interception, when the value of key is undefined, this method is called, and if you write this method yourself, the value of key is called here.
The most common use of KVC is in serializing and deserializing session objects. Especially when it comes to JSON-to-model, KVC lets programmers get rid of tedious, nutrient-free code buildup. Reducing the amount of code is the probability of reducing errors
2)
When using KVC to assign values or values to an object, it is necessary to know the exact key value, compared with the point syntax, KVC is an indirect way of passing, which is advantageous to
Objects are decoupled so that the coupling between the objects is not too high.
KVC can not only assign values, but also break read-only features. The KVC operation in OC and the variable that uses the reflection mechanism in Java to access the private permissions of the class, is violent, which destroys the encapsulation of the class. Both the private and read-only properties of a class can be accessed through KVC.
Of course this is just one of the details we need to pay attention to, and more importantly, KVC has the function of automatic boxing (automatic type conversion), we do not need to convert the type.
3), support key-value path (keypath)
What is a supported key-value path? Key-value paths are handy for properties that have array objects in a class
To be blunt is to support nesting. Because the class key is nested repeatedly, so there is a keypath concept, KeyPath is to use the. Number to link a key, so that it can be accessed according to the path (KVC in accordance with the value of the key-value path, will automatically layer deep, get the corresponding key value)
4) Support operator
If we have 10 strings, we want to find out the total length of these 10 strings, we can use the operator provided by KVC.
Nsarray *[email protected][@ "Bird's Barbecue private dish" @ "Programmer's pancake Treasure", @ "Basic course of soup";
NSLog (@ "%@", [Books valueforkeypath:@ "@sum. Length"]);
The @sum here is provided by KVC, not by our writing. Functions like this have a total of 5 @avg, @count, @max, @min, @sum. We can use it directly.
5) Disadvantages of KVC:
Once you use KVC, your compiler cannot check for errors, that is, the keys and key paths you set are not checked for errors, and the execution is less efficient than the composite accessor method and the custom setter and Getter methods. Because you use the KVC key-value encoding, it must parse the string first, and then set or access an instance variable of the object. So use it only when you really need the extensibility it provides.
—————————————————————————————————————————————————————————————
The three KVC mechanism finds the principle of value by key
When invoking an object through KVC, such as: [Self valueforkey:@ "Somekey"], the program automatically attempts to parse the call in several different ways.
First find whether the object with Somekey this method, if not found, will continue to find whether the object with Somekey this instance variable (iVar), if not found, the program will continue to attempt to invoke-(ID) Valueforundefinedkey: This method. If this method is not implemented, the program throws a Nsundefinedkeyexception exception error.
Add: KVC Find the method, not only will find Somekey this method, but also find Getsomekey this method, preceded by a get, or _somekey in the form of _getsomekey. At the same time, finding the instance variable will not only look for the variable somekey, but also find out if the _somekey variable exists.
Design Valueforundefinedkey: The main purpose of the method is that when you use the-(ID) Valueforkey method to request a value from an object, the object can have a last chance to respond to the request before the error occurs.
---KVC full solution for iOS development