1, KVC (key value code) "1", what is KVC?
- KVC commonly known as key-value encoding, is the ability to pass through a series of methods (
KVC方法 ) According to the object's property name as the key value, the specific value of the values assigned to the property of the object.
"2", the role of KVC
- Typically, when the key in the dictionary is exactly the same as the property name of the object, this is the property that we can set to the object by using the value of the KVC in the dictionary, but remember that the attribute in the object can only be more than the key in the dictionary, and once the key in the dictionary cannot find its corresponding property in the object, program crashes; This is one of the most deadly shortcomings of KVC.
"3", KVC three kinds of forms
2. Precautions to use KVC:
-
You must not include a path by using KVC, assign a value to an object property, or take a value from an object based on a key, because, without Path the KVC method, the passed string is treated as a full key by default. The path is not recognized (that is, the. symbol in key), or if the incoming path program crashes.
-
Arrays using KVC do not include a path, only the ability to calculate the length of an array, no other action, or the program crashes.
-
Perform the function of calculating the length of the array:
-
The program crashes when you perform the Calculate length feature and add a property name to the key:
-
Execute SUM function, program crashes:
-
When the array uses the KVC Forkeypath method to calculate the length, the property name of the incoming object is invalid; Returns the length of the entire array:
Summarize:
-
KVC cannot pass in the path when using the Forkey type method, because the Forkey type method only treats the passed-in string as the entire key, and when the object finds the property, it does not find an error with the same name as the key.
-
Arrays can only perform the function of calculating length when using the Forkey type method, namely: Array.count, however, the @ symbol must be used in key to indicate that this is performing a calculation operation.
-
KVC when using the Forkeypath type method, the runtime system can find the property of the corresponding object according to the path in key, and perform assignment and value operation.
-
KVC when using the Forkeypath type method, it can be added to the key in conjunction with the @ sign to perform the calculation operation.
-
When a dictionary uses KVC, it can be set to the corresponding property of the object according to the value of the key in the dictionary, or the properties of the object can be packaged into a dictionary based on an array of object properties.
iOS Core note-KVC mechanism