除了一般的赋值和取值的方法,我们还可以用Key-Value-Coding(KVC)键值编码来访问你要存取的类的属性。
下图来自苹果官网:
如何使用KVC存取对象属性呢?看个示例
1、使用KVC
定义一个Student类,继承于NSObject。
.h文件
1. #import <Foundation/Foundation.h>
2.
3. @interface Student : NSObject
4. {
5. NSString *name;
6. }
7. @end
.m文件
1. #import "Student.h"
2. @implementation Student
3. @end
.m文件也没有实现。name属性没有加property,原来的访问方法就访问不了name属性了。怎么办呢?用kvc就可以了
1. #import "Student.h"
2. intmain(intargc, constchar* argv[])
3. {
4. @autoreleasepool {
5. Student *student = [[[Student alloc]init ]autorelease];
6. [student setValue:@"张三"forKey:@"name"];
7. NSString *name = [student valueForKey:@"name"];
8. NSLog(@"学生姓名:%@",name);
9. }
10. return0;
11. }
打印结果:
2012-07-20 15:04:09.920 objectiveC[1977:403] 学生姓名:张三
张三 这个值存进去了,通过valueForKey取出来了。
如果存的时候key和类属性的名称不一致会怎么样呢?
[student setValue:@"张三" forKey:@"name1"];
运行,程序崩溃 ,打印:
2012-07-20 15:09:40.432 objectiveC[2069:403] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Student 0x106f14270> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name1.'
提示没有这个name1 这个key。
2、键路径访问属性
关键字:键路径取值valueForKeyPath 键路径存值:forKeyPath
新建一个类Course,课程类,课程类有课程名称这个属性
.h文件
1. #import <Foundation/Foundation.h>
2.
3. @interface Course : NSObject
4. {
5. NSString *CourseName;
6. }
7. @end
.m文件
1. #import "Student.h"
2. @implementation Student
3. @end
在Student中添加Course属性 ,student.h文件中代码如下:
1. #import <Foundation/Foundation.h>
2. @classCourse;
3. @interface Student : NSObject
4. {
5. NSString *name;
6. Course *course;
7. }
8. @end
实现还是什么都没有,这里就不贴代码了
在main方法中,我们实验通过键路径访问Course中CourseName的属性
1. #import "Student.h"
2. #import "Course.h"
3. intmain(intargc, constchar* argv[])
4. {
5. @autoreleasepool {
6. Student *student = [[[Student alloc]init ]autorelease];
7. [student setValue:@"张三"forKey:@"name"];
8. NSString *name = [student valueForKey:@"name"];
9. NSLog(@"学生姓名:%@",name);
10.
11. Course *course = [[[Course alloc]init] autorelease];
12. [course setValue:@"语文课"forKey:@"CourseName"];
13. [student setValue:course forKey:@"course"];
14. NSString *courseName = [student valueForKeyPath:@"course.CourseName"];
15. NSLog(@"课程名称:%@", courseName);
16.
17. //也可以这样存值
18. [student setValue:@"数学课"forKeyPath:@"course.CourseName"];
19. courseName = [student valueForKeyPath:@"course.CourseName"];
20. NSLog(@"课程名称:%@", courseName);
21.
22. }
23. return0;
24. }
运行打印结果:
2012-07-20 15:33:51.902 objectiveC[2415:403] 学生姓名:张三
2012-07-20 15:33:51.904 objectiveC[2415:403] 课程名称:语文课
2012-07-20 15:33:51.904 objectiveC[2415:403] 课程名称:数学课
3、自动封装基本数据类型
我们在Student类中添加分数属性 NSInteger point;
.h文件
1. #import <Foundation/Foundation.h>
2. @classCourse;
3. @interface Student : NSObject
4. {
5. NSString *name;
6. Course *course;
7. NSInteger point;
8. }
9. @end
.m文件不改变就不管了
下面是main示例了
1. #import "Student.h"
2. #import "Course.h"
3.
4. intmain(intargc, constchar* argv[])
5. {
6. @autoreleasepool {
7. Student *student = [[[Student alloc]init ]autorelease];
8. [student setValue:@"张三"forKey:@"name"];
9. NSString *name = [student valueForKey:@"name"];
10. NSLog(@"学生姓名:%@",name);
11.
12. Course *course = [[[Course alloc]init] autorelease];
13. [course setValue:@"语文课"forKey:@"CourseName"];
14. [student setValue:course forKey:@"course"];
15. NSString *courseName = [student valueForKeyPath:@"course.CourseName"];
16. NSLog(@"课程名称:%@", courseName);
17.
18. //也可以这样存值
19. [student setValue:@"数学课"forKeyPath:@"course.CourseName"];
20. courseName = [student valueForKeyPath:@"course.CourseName"];
21. NSLog(@"课程名称:%@", courseName);
22.
23. [student setValue:@"88"forKeyPath:@"point"];
24. NSString *point = [student valueForKey:@"point"];
25. NSLog(@"分数:%@", point);
26.
27. }
28. return0;
29. }
打印结果:
2012-07-20 15:43:19.593 objectiveC[2533:403] 学生姓名:张三
2012-07-20 15:43:19.596 objectiveC[2533:403] 课程名称:语文课
2012-07-20 15:43:19.596 objectiveC[2533:403] 课程名称:数学课
2012-07-20 15:43:19.598 objectiveC[2533:403] 分数:88
我们用NSString*类型设置的属性值@"88",而我们的属性是NSInteger类型的,存取都没有问题。
4、操作集合
在Student类中加入数组NSArray,用来表示其他的学生。这样我们可以添加多个其他的学生,再用集合操作计算学生的分数,最高分,最低分,平均分等
1. #import <Foundation/Foundation.h>
2. @classCourse;
3. @interface Student : NSObject
4. {
5. NSString *name;
6. Course *course;
7. NSInteger point;
8. NSArray *otherStudent;
9. }
10. @end
.m文件实现不变。
在main函数中添加三个学生,添加到数组中,然后求平均分,最高,最低分,学生数量
1. #import "Student.h"
2. #import "Course.h"
3. intmain(intargc, constchar* argv[])
4. {
5. @autoreleasepool {
6. Student *student = [[[Student alloc]init ]autorelease];
7. [student setValue:@"张三"forKey:@"name"];
8. NSString *name = [student valueForKey:@"name"];
9. NSLog(@"学生姓名:%@",name);
10.
11. [student setValue:@"88"forKey:@"point"];
12. NSString *point = [student valueForKey:@"point"];
13. NSLog(@"分数:%@", point);
14.
15. Student *student1 = [[[Student alloc]init]autorelease];
16. Student *student2 = [[[Student alloc]init]autorelease];
17. Student *student3 = [[[Student alloc]init]autorelease];
18. [student1 setValue:@"65"forKey:@"point"];
19. [student2 setValue:@"77"forKey:@"point"];
20. [student3 setValue:@"99"forKey:@"point"];
21. NSArray *array = [NSArray arrayWithObjects:student1,student2,student3,nil];
22. [student setValue:array forKey:@"otherStudent"];
23. NSLog(@"其他学生的成绩%@", [student valueForKeyPath:@"otherStudent.point"]);
24. NSLog(@"共%@个学生", [student valueForKeyPath:@"otherStudent.@count"]);
25. NSLog(@"最高成绩:%@", [student valueForKeyPath:@"otherStudent.@max.point"]);
26. NSLog(@"最低成绩:%@", [student valueForKeyPath:@"otherStudent.@min.point"]);
27. NSLog(@"平均成绩:%@", [student valueForKeyPath:@"otherStudent.@avg.point"]);
28. }
29. return0;
30. }
运行打印结果
2012-07-20 16:09:17.101 objectiveC[2857:403] 学生姓名:张三
2012-07-20 16:09:17.104 objectiveC[2857:403] 分数:88
2012-07-20 16:09:17.105 objectiveC[2857:403] 其他学生的成绩(
)
2012-07-20 16:09:17.106 objectiveC[2857:403] 共3个学生
2012-07-20 16:09:17.106 objectiveC[2857:403] 最高成绩:99
2012-07-20 16:09:17.107 objectiveC[2857:403] 最低成绩:65
2012-07-20 16:09:17.108 objectiveC[2857:403] 平均成绩:80.333333333333333333333333333333333333
@sum。