KVC Introduction
KVC (key value coding) is also called key/value encoding. The basic calls in key/value encoding are-valueforkye: And-setvalue: forkey :.
Example: define a student class. The student. h header file is as follows:
# Import <Foundation/Foundation. h> // student class @ interface Student: nsobject {nsstring * Name; int age ;}@ end
The student. m implementation file is as follows:
# Import "student. H "@ implementation student-(nsstring *) Description {return [nsstring stringwithformat: @" Name: % @, age: % d ", name, age];} @ end
The get and set methods are not added to the name and age attributes. You can use KVC to set the attribute values.
The main. m code is as follows:
# Import <Foundation/Foundation. h> # import "student. H "int main (INT argc, const char * argv []) {@ autoreleasepool {student * student1 = [[STUDENT alloc] init]; [student1 setvalue: @ "Lily" forkey: @ "name"]; [student1 setvalue: [nsnumber numberwithint: 20] forkey: @ "Age"]; nslog (@ "% @", student1); nslog (@ "student age: % @", [student1 valueforkey: @ "Age"]);} return 0 ;}
The output result is as follows:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/45/9B/wKiom1Po6fSj7dUFAACiXzr8Q6c816.jpg "Title =" screen snapshot noon 12.04.56.png "alt =" wkiom1po6fsj7dufaacixzr8q6c816.jpg "/> from the results we can see that we successfully modified the attribute values of name and age, however, it is worth noting that age is an int value, not an object. For KVC, cocoa automatically puts and retrieves the scalar value. When we call-setvalue: forkey: it automatically extracts the scalar value from the object and assigns it to age. When we call-valueforkey:, it automatically puts the scalar value into nsnumber or nsvalue.
2. Key Path
Key-value encoding also supports specifying the Key Path
Add a new class grade class. Multiple students in a class are represented by nsmutablearray * students. The grade. h header file is as follows:
# Import <Foundation/Foundation. h> // class @ interface grade: nsobject {nsstring * Name; nsmutablearray * students;} @ property (nonatomic, copy) nsstring * Name; @ property (nonatomic, retain) nsmutablearray * students; @ end
The implementation of grade. m is as follows:
# Import "grade. H "@ implementation grade @ synthesize name; @ synthesize students;-(nsstring *) Description {nsmutablestring * STR = [[nsmutablestring alloc] init]; [STR appendformat: @ "Class Name: % @ \ n", name]; [STR appendstring: @ "student list: \ n"]; for (id obj in students) {[STR appendformat: @ "% @ \ n", OBJ];} return STR ;}@ end
Main. m is as follows:
# Import <Foundation/Foundation. h> # import "student. H "# import" grade. H "int main (INT argc, const char * argv []) {@ autoreleasepool {student * student1 = [[STUDENT alloc] init]; [student1 setvalue: @ "Lily" forkey: @ "name"]; [student1 setvalue: [nsnumber numberwithint: 20] forkey: @ "Age"]; nslog (@ "% @", student1); nslog (@ "student age: % @", [student1 valueforkey: @ "Age"]); Student * student2 = [[STUDENT alloc] init]; [student2 setvalue: @ "lilei" forkey: @ "name"]; [student2 setvalue: [nsnumber numberwithint: 19] forkey: @ "Age"]; nslog (@ "% @", student2); nsmutablearray * array = [[nsmutablearray alloc] init]; [array addobject: student1]; [array addobject: student2]; grade * grade = [[grade alloc] init]; grade. name = @ "Second Class"; grade. students = array; nslog (@ "% @", grade); nsarray * studentnames = [grade valueforkeypath: @ "students. name "]; nslog (@" % @ ", studentnames); nsnumber * sumage = [grade valueforkeypath: @" [email protected] "]; nslog (@" Total Age: % @ ", sumage); nsnumber * avgage = [grade valueforkeypath: @" [email protected] "]; nslog (@" Average age: % @ ", avgage ); nsnumber * maxage = [grade valueforkeypath: @ "[email protected]"]; nslog (@ "maximum age: % @", maxage); nsnumber * minage = [grade valueforkeypath: @ "[email protected]"]; nslog (@ "Minimum Age: % @", minage);} return 0 ;}
We define two students, add them to a variable array, and assign values to the student attributes of the grade.
NSArray *studentNames = [grade valueForKeyPath:@"students.name"];
To obtain the names of all students at a time.
Key paths can also reference some operators for some operations, such as the sum, average, maximum, and minimum of the age of the class students.
The running result is as follows:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/45/9D/wKioL1Po8AXAWcoJAAGrxIPTfUs572.jpg "Title =" screen snapshot at noon 12.22.23.png "alt =" wkiol1po8axawcojaagrxiptfus572.jpg "/>
Note: KVC needs to parse the string to calculate the answer you need, so the speed is slow and the compiler cannot perform error checks on it. For example, to obtain the student's Gender
NSArray *studentSexs = [grade valueForKeyPath:@"students.sex"];NSLog(@"%@",studentSexs);
But the compiler cannot determine whether it is a wrong key path. Therefore, when you try to use it, a runtime error occurs, such:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/45/9D/wKiom1Po8s_ic5AXAANMZ83v0YQ917.jpg "Title =" screen snapshot last noon 12.42.50.png "alt =" wkiom1po8s_ic5axamz83v0yq917.jpg "/>
So much KVC is used ~
This article is from the "7803002" blog, please be sure to keep this source http://7813002.blog.51cto.com/7803002/1538811