KVC Full solution of iOS

Source: Internet
Author: User


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 property of the object according to the value of key Setnilvalueforkey: When you set nil on a non-class object property, the default throws an exception. 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 you use KVC to assign values or values, you need to know the exact key value, compared to the point syntax, KVC is an indirect way of delivery, which facilitates
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
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) KVC disadvantage: Once using KVC your compiler cannot check for errors, that is, the set key, key path is not checked for error, and execution efficiency is lower than the composite accessor method and the custom setter and getter method. 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. —————————————————————————————————————————————————————————————three KVC mechanism to find 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 of iOS

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.