Key-Value Coding (KVC) in IOS

Source: Internet
Author: User

Key-Value Coding (KVC) in IOS

Kvc is an informal protocol that provides a mechanism to indirectly access the attributes of an object. Direct access to objects is implemented by calling the accessors method. kvc can directly access the properties of objects without calling the accessors setting and obtaining methods.

The following describes how to use kvc:

1. assign values to attributes

Kvc sets the attribute value through key-value pairs and provides the following method, which is equivalent to the set Method in the accessors. Value is the value to be set, key is a string, and the content in the string is the attribute name

 
- (void)setValue:(id)value forKey:(NSString *)key; - (void)setValue:(id)value forKeyPath:(NSString *)keyPath;

The following uses a small example to illustrate how to assign values to attributes through the accessors and kvc.

Assuming that the Student class has a name attribute, the method of directly assigning values to the name is:

 
Student *stu = [[Student alloc] init]; stu.name = @jerehedu;

Assign a value to the name using kvo:

 
Student *stu = [[Student alloc] init]; [stu setValue:@jerehedu forKey:@name];

Assume that the student class has another object stuClass whose attribute is the ClassInfo class. In the ClassInfo class number, set the class number directly in the following way:

 
stu.stuClass = [ClassInfo new]; stu.stuClass.classNo = 1;

Set the class number through kvc:

 
stu.stuClass = [ClassInfo new]; [stu setValue:@(1) forKeyPath:@stuClass.classNo];

When keyPath is set, the path is represented by xx. xx;

The value of value is an OC object. If the value is of the basic data type, it needs to be packed into an OC object;

2. Get the attribute value

Kvc provides a way to get the object property value, which is equivalent to the get method of the accessor. The key-Value Pair method is also used to obtain the value.

 
- (id)valueForKey:(NSString *)key; - (id)valueForKeyPath:(NSString *)keyPath;

The following uses a small example to explain how to use the accessor and kvc code to set the attribute values.

Assume that you want to obtain the value of name in the Student class by using the following method:

 
NSString *name = stu.name;

The value of kvc is as follows:

 
NSString *name = [stu valueForKey:@name];

Use the accessors method to retrieve the class number from the Student class directly:

 
int num = stu.stuClass.classNo;

Use kvc to obtain the class number in the Student class:

 
int num = [[stu valueForKeyPath:@stuClass.classNo] intValue];

The default value is the OC object. If you want to obtain the basic data type, you need to unpack it.

3. If the key cannot be found, an exception is returned.

When kvc is used, if the key value in the Code does not exist, an exception is thrown. You can rewrite it in the class to provide the following method to solve this problem.

 
- (void)setValue:(id)value forUndefinedKey:(NSString *)key;

When the key does not exist, the above method is automatically called and can be processed in this method.

4. supplement the usage of kvc

Kvc is very easy to use and can simplify our code. In addition to directly accessing attributes without using the accesser method, kvc can also perform operations on attributes in objects.

For example, if you add a new Test class, which has a score attribute, there is an array in the Student class, And the array stores the Test information (Test class object ), through kvo, you can directly obtain the average scores and the best scores of all exams stored in the array.

Store exam array information through kvo:

 
NSMutableArray *ary = [NSMutableArray array]; for (int i=0; i<5; i++) { Test *test = [[Test alloc] init]; [test setValue:@(100-10*i) forKey:@score]; [ary addObject:test]; } stu.testAry = ary;

Print the exam information in Student:

 
 
NSLog (@ show: testAry =%@, [self valueForKey: @ testAry]); NSLog (@ show: testAry. score =%@, [self. testAry valueForKey: @ score]); NSLog (@ show: testAry. score =%@, [self. testAry valueForKeyPath: @ score]); NSLog (@ show: total score in the array: SUMscore =%@, [self. testAry valueForKeyPath: @ sum. score]); NSLog (@ show2: AVGscore = % @, [self. testAry valueForKeyPath: @ avg. score]); NSLog (@ show2: MAXscore in the array = % @, [self. testAry valueForKeyPath: @ max. score]); NSLog (@ show2: min score in the array =%@, [self. testAry valueForKeyPath: @ min. score]);

 

 

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.