KVC Full solution of iOS development

Source: Internet
Author: User
Tags throw exception


1, KVC is the key value coding abbreviation, meaning is the key code.


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 <1> read the properties of an object based on the value of key
-(ID) Valueforkey: (NSString *) key; <2> write properties of an object based on the value of key
-(void) SetValue: (ID) value Forkey: (NSString *) key; <3> when you set nil on a non-class object property, the default throw exception Setnilvalueforkey: <4> error Intercept, when the value of key is undefined, this method will be called, if you write this method yourself, An error in the value of key will be called here to Valueforundefinedkey:

KVC most commonly used in serialization and deserialization of a Session object。 Especially in the JSON-to-model, KVC allows programmers to get rid of the tedious and nutrient-free code buildup and reduce the amount of code to reduce the probability of errors.

2, use KVC for the object assignment or value, need to know the exact key value, compared to the point syntax, KVC is an indirect way of transmission, this way is conducive to the object decoupling, so that the coupling between the object 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 be aware of, and more importantly KVC hasAutomatic Boxing(Automatic type conversion), we do not need to convert the type.

3, support key-value path (1) What is called the Support key value path? A key-value path is a convenient way to have an array object's properties in a Class (2) to be blunt is to support nesting. Because 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 this path (KVC The value of the key-value path, it will automatically layer in depth, get the corresponding key value)

4, support operator (1) 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.

(2) Nsarray *[email protected][@ "Bird's Barbecue Private cuisine" @ "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 shortcomings: (1) Once you use KVC your compiler cannot check for errors, that is, the set of keys, key paths are not checked for errors, (2) and the 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. Third, KVC mechanism through key to find the principle of value1. When invoking an object through KVC, for example: [Self valueforkey:@ "Somekey"], the program automatically attempts to parse the call in several different ways. 2, 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. 3, add: KVC Find method, not only will find Somekey this method, but also find Getsomekey this method, preceded by a get, or _somekey to _getsomekey in several forms. 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. 4, 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 (without collapsing) before the error occurs. 5. The Setvaluesforkeyswithdictionary method is also called the Setvalueforkey method

KVC Full solution of iOS development

Related Article

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.