iOS Core note-KVC mechanism

Source: Internet
Author: User

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
    • The path is not included in key:

      • Use setValue:forKey: methods to assign values to object properties:

      • Use valueForKey the method to remove the object's property values:

      • The array uses the valueForKey method and the key does not contain a path, and the corresponding properties of all objects in the array are extracted to form a new array to return:

    • The key contains the path:

      • Use setValue:forKeyPath: methods to assign values to object properties:

      • Use valueForKeyPath the method to remove the object's property values:

      • In the same way, arrays are used to valueForKeyPath extract the key attributes of the objects in the array to form a new array;

      • Use the array valueForKeyPath method, add the @ symbol to the key + the calculation operation to perform, you can implement the specified calculation function:

    • Dictionary using KVC:

      • The dictionary uses a setValuesForKeysWithDictonary: method that sets the value of the key in the dictionary to the corresponding property of the object, but remember that the key in the dictionary must have a corresponding attribute in the object (that is, the attribute in the object can only be more than the key in the dictionary), if the key in the dictionary cannot find the corresponding property in the object , the program crashes.

      • Object uses a dictionaryWithValuesForKeys: method that returns a dictionary by passing in an array of properties of the object, storing the properties of the object as key-value pairs in the dictionary.

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

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.