In addition to the general assignment and value methods, we can also use key-value-coding (KVC) Key-value encoding to access the attributes of the class you want to access.
From Apple's official website:
How to Use KVC to access object attributes? See example
1. Use KVC
Defines a student class that inherits from nsobject.
. H file
#import <Foundation/Foundation.h>@interface Student : NSObject{ NSString *name;}@end
. M file
#import "Student.h"@implementation Student@end
The. M file is not implemented either. If the name attribute is not added with property, the original access method cannot access the name attribute. What should we do? You can use KVC.
#import "Student.h"int main(int argc, const char * argv[]){ @autoreleasepool { Student *student = [[[Student alloc]init ]autorelease]; [student setValue:@"张三" forKey:@"name"]; NSString *name = [student valueForKey:@"name"]; NSLog(@"学生姓名:%@",name); } return 0;}
Print result:
15:04:09. 920 objectivec [1977: 403] Student name: James
The value of Michael Jacob is saved and retrieved through valueforkey.
What if the names of key and class attributes are inconsistent during storage?
Code changed
[STUDENT setvalue: @ "Zhang San" forkey: @ "name1"];
Run, program crash, print:
15:09:40. 432 objectivec [2069: 403] *** terminating app due to uncaught exception 'nsunknownkeyexception', reason: '[<student 0x0000f14270> setvalue: forundefinedkey:]: this class is not key value coding-compliant for the key name1 .'
The key name1 is not found.
2. Key Path access attributes
What if I access the attributes in this class? The Key Path is used.
Keywords: Key Path value valueforkeypath Key Path stored value: forkeypath
Create a new course class. The course class has the course name attribute.
. H file
#import <Foundation/Foundation.h>@interface Course : NSObject{ NSString *CourseName;}@end
. M file
#import "Student.h"@implementation Student@end
Add the course attribute to student. The code in student. H is as follows:
#import <Foundation/Foundation.h>@class Course;@interface Student : NSObject{ NSString *name; Course *course;}@end
There is nothing to implement, so no code will be posted here.
In the main method, we use the Key Path to access the coursename attribute in course.
#import "Student.h"#import "Course.h"int main(int argc, const char * argv[]){ @autoreleasepool { Student *student = [[[Student alloc]init ]autorelease]; [student setValue:@"张三" forKey:@"name"]; NSString *name = [student valueForKey:@"name"]; NSLog(@"学生姓名:%@",name); Course *course = [[[Course alloc]init] autorelease]; [course setValue:@"语文课" forKey:@"CourseName"]; [student setValue:course forKey:@"course"]; NSString *courseName = [student valueForKeyPath:@"course.CourseName"]; NSLog(@"课程名称:%@", courseName); //也可以这样存值 [student setValue:@"数学课" forKeyPath:@"course.CourseName"]; courseName = [student valueForKeyPath:@"course.CourseName"]; NSLog(@"课程名称:%@", courseName); } return 0;}
Run the print result:
15:33:51. 902 objectivec [2415: 403] Student name: James
15:33:51. 904 objectivec [2415: 403] Course name: Chinese
15:33:51. 904 objectivec [2415: 403] Course name: Mathematics Class
3. Automatically encapsulate Basic Data Types
We add the score attribute nsinteger point in the student class;
. H file
#import <Foundation/Foundation.h>@class Course;@interface Student : NSObject{ NSString *name; Course *course; NSInteger point;}@end
The. M file will not be changed.
The following is the main example.
#import "Student.h"#import "Course.h"int main(int argc, const char * argv[]){ @autoreleasepool { Student *student = [[[Student alloc]init ]autorelease]; [student setValue:@"张三" forKey:@"name"]; NSString *name = [student valueForKey:@"name"]; NSLog(@"学生姓名:%@",name); Course *course = [[[Course alloc]init] autorelease]; [course setValue:@"语文课" forKey:@"CourseName"]; [student setValue:course forKey:@"course"]; NSString *courseName = [student valueForKeyPath:@"course.CourseName"]; NSLog(@"课程名称:%@", courseName); //也可以这样存值 [student setValue:@"数学课" forKeyPath:@"course.CourseName"]; courseName = [student valueForKeyPath:@"course.CourseName"]; NSLog(@"课程名称:%@", courseName); [student setValue:@"88" forKeyPath:@"point"]; NSString *point = [student valueForKey:@"point"]; NSLog(@"分数:%@", point); } return 0;}
Print result:
15:43:19. 593 objectivec [2533: 403] Student name: James
15:43:19. 596 objectivec [2533: 403] Course name: Chinese
15:43:19. 596 objectivec [2533: 403] Course name: Mathematics Class
15:43:19. 598 objectivec [2533: 403] Score: 88
We use the nsstring * type to set the attribute value @ "88", and our attribute is of the nsinteger type, so there is no problem with access.
4. Operation set
Add an array nsarray to the student class to indicate other students. In this way, we can add multiple other students and use the set operation to calculate the scores, highest score, lowest score, and average score of the students.
#import <Foundation/Foundation.h>@class Course;@interface Student : NSObject{ NSString *name; Course *course; NSInteger point; NSArray *otherStudent;}@end
The. M file implementation remains unchanged.
Add three students to the main function, add them to the array, and calculate the average score, maximum score, lowest score, and number of students.
#import "Student.h"#import "Course.h"int main(int argc, const char * argv[]){ @autoreleasepool { Student *student = [[[Student alloc]init ]autorelease]; [student setValue:@"张三" forKey:@"name"]; NSString *name = [student valueForKey:@"name"]; NSLog(@"学生姓名:%@",name); [student setValue:@"88" forKey:@"point"]; NSString *point = [student valueForKey:@"point"]; NSLog(@"分数:%@", point); Student *student1 = [[[Student alloc]init]autorelease]; Student *student2 = [[[Student alloc]init]autorelease]; Student *student3 = [[[Student alloc]init]autorelease]; [student1 setValue:@"65" forKey:@"point"]; [student2 setValue:@"77" forKey:@"point"]; [student3 setValue:@"99" forKey:@"point"]; NSArray *array = [NSArray arrayWithObjects:student1,student2,student3,nil]; [student setValue:array forKey:@"otherStudent"]; NSLog(@"其他学生的成绩%@", [student valueForKeyPath:@"otherStudent.point"]); NSLog(@"共%@个学生", [student valueForKeyPath:@"[email protected]"]); NSLog(@"最高成绩:%@", [student valueForKeyPath:@"[email protected]"]); NSLog(@"最低成绩:%@", [student valueForKeyPath:@"[email protected]"]); NSLog(@"平均成绩:%@", [student valueForKeyPath:@"[email protected]"]); } return 0;}
Run the printed result
16:09:17. 101 objectivec [2857: 403] Student name: James
16:09:17. 104 objectivec [2857: 403] Score: 88
16:09:17. 105 objectivec [2857: 403] scores of other students (
65,
77,
99
)
16:09:17. 106 objectivec [2857: 403] Three students
16:09:17. 106 objectivec [2857: 403] maximum score: 99
16:09:17. 107 objectivec [2857: 403] lowest score: 65
16:09:17. 108 objectivec [2857: 403] average score: 80.333333333333333333333333333333333333
You can also calculate the sum @ sum.
Key-value-coding (KVC)