[Objective-C] OC key value encoding (KVC concept and usage)

Source: Internet
Author: User

 

Basic concepts of key-value Encoding

1: Key-value encoding is a mechanism used to introduce Access Object Attributes. Using this mechanism, you do not need to call access methods and variable instances.

You can access object attributes.

2: The key-value encoding method is declared in the Objective-C Informal Association (Category) NSKeyValueCoding. The default Implementation Method

Provided by NSObject.

3: Key-value encoding supports attributes with the object name, as well as numerical types and structures. Non-object parameters and return types are

Identify and automatically encapsulate/unseal.

Settings and access:

Basic calls for key-value encoding flood include the-valueForKey: And setValue: forKey: Methods in the form of strings.

Sending messages to objects. strings are the key to our attention to attributes. Let's take a look at the following example of settings and access:

 

Person * person = [[Person alloc] init];

NSMutableString * name = [[NSMutableString alloc] initWithString: @ "James"];

[Person setValue: name forKey: @ "name"];

NSLog (@ "James name: % @", [person valueForKey: @ "name"]);

First, check whether the setter and getter methods exist. If they do not exist, it searches for instance variables named _ key or key internally. If

 

If no, an error will be reported. KVC can be used to obtain the object value without the getter method, without direct access through the object pointer.

[Note]: When we use setValue: forKey: to set the object value or valueForKey: to obtain the object value, if

When the instance variables of the image are basic data types (for example, char, int, float, and BOOL), we need to encapsulate these data types.

 

Path and many-to-many relationship

Path:

In addition to setting and accessing through the key value above, the key value also supports specifying a path, like a file. Separated. Example:

 

Person * person = [[Person alloc] init];

Auther * auther = [[Auther alloc] init];

[Auther setValue: @ "IOS development" forKey: @ "title"];

NSMutableString * name = [[NSMutableString alloc] initWithString: @ "James"];

[Person setValue: name forKey: @ "name"];

[Person setValue: auther forKey: @ "auther"];

NSLog (@ "name: % @", [person valueForKey: @ "name"]);

NSString * title = [person valueForKeyPath: @ "auther. title"];

NSLog (@ "title: % @", title );

 

One-to-multiple relationship:

If you request a key value from NSArray, it actually queries every object in the array to find the key value, and then returns the query result

Package it into another array and return it to you. Example:

// One-to-multiple relationship

NSMutableArray * persons = [NSMutableArray arrayWithCapacity: 3];

For (int I = 0; I <3; I ++ ){

Person * peson1 = [[Person alloc] init];

NSString * name = [NSString stringWithFormat: @ "person _ % d", I];

[Peson1 setValue: name forKey: @ "name"];

[Persons addObject: peson1];

[Peson1 release];

}

[Person setValue: persons forKey: @ "rePerson"];

NSArray * nameArray = [person valueForKeyPath: @ "rePerson. name"];

NSLog (@ "% @", nameArray );

 

 

Simple operations of KVC:

In addition, some characters can be used for simple operations. Sum, min, max, avg, count

 

// Add NSString * sum = [person valueForKeyPath: @ "rePerson.@sum.age"]; NSLog (@ "sum = % @", sum ); // quantity NSString * count = [person valueForKeyPath: @ "rePerson.@count.age"]; NSLog (@ "count = % @", count ); // maximum NSString * max = [person valueForKeyPath: @ "rePerson.@max.age"]; NSLog (@ "max = % @", max ); // minimum NSString * min = [person valueForKeyPath: @ "rePerson.@min.age"]; NSLog (@ "min = % @", min ); // average NSString * avg = [person valueForKeyPath: @ "rePerson.@avg.age"]; NSLog (@ "avg = % @", avg );

 

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.