Objective-c in KVC (a) (view, portable, easy to understand, original pure hand-crafted version)

Source: Internet
Author: User

Use of KVC

1, KVC full name key valued coding key value code

The reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods and properties. Java,c# all have this mechanism. OBJC also have, so you do not have to do anything at the root of the dynamic read and write properties, that is, KVC.

The KVC method is provided by Nskeyvaluecoding, which is the NSObject category, which means that almost all objects in OBJC support KVC operations.

2. Common methods

Methods to get values

Valueforkey:, the name of the incoming NSString property.

Valueforkeypath:, the path of the incoming NSString property, xx.xx form.

Valueforundefinedkey its default implementation is to throw an exception, you can override this function to do error handling.

Ways to modify values

Setvalue:forkey:

Setvalue:forkeypath:

Setvalue:forundefinedkey:

Setvalue:forkey's search process:

1. First search Set<key>: Method

If the member is using @property, @synthsize processing because @synthsize tells the compiler to automatically generate Set<key>: The setter method of the format, so this case will be searched directly.

Note: <Key> here refers to the member name, and the first letter is capitalized.

2. The above setter method is not found, if the class method accessinstancevariablesdirectly returns Yes (note: This is a class method implemented in Nskeyvaluecodingcatogery, the default implementation is to return YES).

Then search for the member names in _<key>,_is<key>,<key>,is<key> order.

3. If the value of the set member is still not found, Setvalue:forundefinedkey is called:.

4. If you do not rewrite the Setvalue:forundefinedkey program will crash immediately.

Attention:

1, when using KVC to indirectly modify the object properties, the system will automatically determine the type of object properties, and complete the conversion.

2. KVC can access member variables, regardless of whether the Getter/setter method is provided, whether or not the visibility is readonly decorated.

application of Setvalue:forundefinedkey and Valueforundefinedkey

The main use of KVC is not only ORM mapping, is to convert dictionary to model, but some servers return the field may be OC keywords such as ' id ', ' description ' and so on. As the code above the example of the ID, we can not let @property after the key value is the ID, so use the uppercase ID instead, KVC is case-sensitive we do not worry. In this case, we can avoid the embarrassment of the keyword by assigning the key value of the ID to the key value of the ID in Setvalue:forundefinedkey.

3, dict <->model Mutual transfer

Dictionary Turn model

[Self setvaluesforkeyswithdictionary:dict];

Model Turn Dictionary

[P Dictionarywithvaluesforkeys:array];

4. KVC Collection

Nsarray/nsset, etc. all support KVC

[Array valueforkeypath:@ "Dog.name"];

5. Using KVC to calculate attributes

The format is: [P valueforkeypath:@ "left keypath part [email protected] section. Right KeyPath part "];

Left KeyPath section: Action object path required.

Collectionoperator section: Use the @ symbol to determine the set operation used.

Right KeyPath section: properties that require collection operations.

Example: [P valueforkeypath:@ "[email protected]"];

@avg: Average

@count: Total

@max: Maximum

@min: Minimum

@sum: Total

Objective-c in KVC (a) (view, portable, easy to understand, original pure hand-crafted version)

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.