键值编码 || Cocoa绑定

论坛 期权论坛 期权     
已经匿名di用户   2022-4-26 16:00   5324   0

<欢迎大家加入iOS开发学习交流群:QQ529560119>

什么是键值编码?

键值编码(Key-Value-Coding)是指,将表示对象包含的信息的字符串作为键值使用,来间接访问该信息的方式。键值编码提供了非常强大的功能,基本上,只要存在访问器方法,生命属性或实例变量,就可以将其名字指定为字符串来访问。

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;
}
复制代码

打印结果:

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,用来表示其他的学生。这样我们可以添加多个其他的学生,再用集合操作计算学生的分数,最高分,最低分,平均分等

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

.m文件实现不变。

在main函数中添加三个学生,添加到数组中,然后求平均分,最高,最低分,学生数量

复制代码
#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:@"otherStudent.@count"]);
        NSLog(@"最高成绩:%@", [student valueForKeyPath:@"otherStudent.@max.point"]);
        NSLog(@"最低成绩:%@", [student valueForKeyPath:@"otherStudent.@min.point"]);
        NSLog(@"平均成绩:%@", [student valueForKeyPath:@"otherStudent.@avg.point"]);
    }
    return 0;
}
复制代码

运行打印结果

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] 其他学生的成绩(

65,

77,

99

)

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。

【键值观察kvo用到的话再说,这里不过多展开论述】

什么是Cocoa绑定?

从Mac os x 10.3起开始引入Cocoa绑定(cocoa binding)是指,使用键值编码和键值观察的组合,在多个对象间共享属性值的变化的机制。

具体不展开论述,有兴趣可深究cocoa绑定。也可发私信询问我cocoa绑定的内容。

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP