您的位置:首页 > 移动开发 > Objective-C

Objective-C语法之KVC使用

2016-07-13 10:28 501 查看


除了一般的赋值和取值的方法,我们还可以用Key-Value-Coding(KVC)键值编码来访问你要存取的类的属性。

下图来自苹果官网:



 
如何使用KVC存取对象属性呢?看个示例
1、使用KVC
定义一个Student类,继承于NSObject。
.h文件

#import <Foundation/Foundation.h>

@interface Student : NSObject
{
NSString *name;
}
@end


.m文件

#import "Student.h"

@implementation Student

@end


.m文件也没有实现。name属性没有加property,原来的访问方法就访问不了name属性了。怎么办呢?用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;
}


打印结果:
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文件

#import <Foundation/Foundation.h>

@interface Course : NSObject
{
NSString *CourseName;
}
@end


.m文件

#import "Course.h"

@implementation Course

@end


在Student中添加Course属性 ,student.h文件中代码如下:

#import <Foundation/Foundation.h>
@class Course;
@interface Student : NSObject
{
NSString *name;
Course *course;
}
@end


实现还是什么都没有,这里就不贴代码了
在main方法中,我们实验通过键路径访问Course中CourseName的属性

#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;
}


运行打印结果:
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文件

#import <Foundation/Foundation.h>
@class Course;
@interface Student : NSObject
{
NSString *name;
Course *course;
NSInteger point;
}
@end


.m文件不改变

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: