KVC, a key-value encoding, is an informal protocol that provides a mechanism to indirectly access the properties of an object. Direct access to an object is implemented by invoking the accessor's method, and KVC does not need to invoke the accessor's settings and get methods, and can directly access the object's properties.
Here's how to use KVC:
1. Assigning values to attributes
KVC is a method of setting a value on a property by means of a key-value pair, providing the following methods, equivalent to the set method in the accessor. Value is the name of the property to be set, key is a string, and the contents of the string are
-(void) SetValue: (ID) value forkey: (NSString *) key; -(void) SetValue: (ID) value forkeypath: (NSString *) KeyPath;
The following is a small example of a code implementation that assigns values to a property through accessors and KVC.
Suppose There is a name attribute in the Student class that assigns a value directly to name:
Student *stu =@ "jerehedu";
Assign a value to name by KVO:
Student *stu = [[Student alloc] init];[ Stu SetValue:@ "jerehedu" forkey:@ "name"];
Suppose the student class also has a property of ClassInfo Class class object Stuclass, in ClassInfo class class has a Classno class number of attributes, following the direct way to set the class number:
New1;
Set the class number by KVC:
New ]; [Stu setvalue:@ (1) Forkeypath:@ "stuclass.classno"];
When set by KeyPath, the path is represented by xx.xx;
Value is the OC object, and if it is a basic data type, it needs to be boxed, that is, to be packaged as an OC object;
2. Get the value of the property
KVC provides a way to get the property value of an object, which is equivalent to the accessor's Get method, which is also a key-value pair when the value is taken.
-(ID) Valueforkey: (NSString *) key; -(ID) Valueforkeypath: (NSString *) KeyPath;
The following is a small example of the code implementation of the property values through accessors and KVC.
Suppose you want to get The value of name in the student class , directly to name:
NSString *name = stu.name;
The way to KVC values is:
NSString *name = [stu Valueforkey:@ "name"];
Take the class number directly from the student class through the accessor method:
int num = stu.stuClass.classNo;
take the class number in the student category by KVC:
int num = [[stu Valueforkeypath:@ "stuclass.classno"] intvalue];
The default value is the OC object, and if you want to get the basic data type, you need to do the unboxing.
3. When key cannot be found, handle exception
When using KVC, if the key value in the code does not exist, an exception is thrown and can be overridden in the class by providing the following method to resolve the problem.
-(void) SetValue: (ID) value forundefinedkey: (NSString *) key;
This method is automatically called when key does not exist, and can be processed in this method.
4, the use of KVC supplement
KVC is very easy to use, simplifying our code, in addition to accessing the properties directly from the accessor method, you can also perform operations on the properties in the object.
For example, a new test class is added, there is a score fraction of the property, in the student class has an array, the array of test information (test class object), through the KVO can be directly stored in the array of all the Test average score, the best results and so on.
Storing the exam array information via KVO:
Nsmutablearray *ary = [Nsmutablearray array]; for (int i=0; i<5; i++) { *test = [[Test alloc] init]; [Test setvalue:@ (*i)forkey:@ "score"]; [ary Addobject:test]; } = ary;
Print out the exam information in the Student class:
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 sumscore in array =%@", [Self.testary Valueforkeypath:@"@sum. Score"] ); NSLog (@"Show2: Average score Avgscore =%@ in array", [Self.testary Valueforkeypath:@"@avg. Score"] ); NSLog (@"show2: Maximum score Maxscore =%@ in array", [Self.testary Valueforkeypath:@"@max. Score"] ); NSLog (@"show2: Minimum score minscore =%@ in array", [Self.testary Valueforkeypath:@"@min. Score"] );
Inquiries or technical exchanges, please join the official QQ Group: (452379712)
Jerry Education
Source:http://www.cnblogs.com/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Use of Key-value Coding (KVC) in iOS