- 浏览: 516368 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
tangyunliang:
大哥你太历害了谢谢
Android基于XMPP Smack Openfire开发IM【四】初步实现两个客户端通信 -
u013015029:
LZ,请问下,在// 添加消息到聊天窗口 , 这里获取Ed ...
Android基于XMPP Smack Openfire开发IM【四】初步实现两个客户端通信 -
endual:
怎么保持会话,我搞不懂啊
Android基于XMPP Smack Openfire开发IM【一】登录openfire服务器 -
donala_zq:
显示:[2013-11-30 11:50:36 - Andro ...
android-----------新浪微博 -
donala_zq:
哥,运行不了啊
android-----------新浪微博
除了一般的赋值和取值的方法,我们还可以用Key-Value-Coding(KVC)键值编码来访问你要存取的类的属性。
下图来自苹果官网:
[img]
[/img]
如何使用KVC存取对象属性呢?看个示例
1、使用KVC
定义一个Student类,继承于NSObject。
.h文件
.m文件
.m文件也没有实现。name属性没有加property,原来的访问方法就访问不了name属性了。怎么办呢?用kvc就可以了
打印结果:
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文件
.m文件
在Student中添加Course属性 ,student.h文件中代码如下:
实现还是什么都没有,这里就不贴代码了
在main方法中,我们实验通过键路径访问Course中CourseName的属性
24.}
#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文件
.m文件不改变就不管了
下面是main示例了
打印结果:
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,用来表示其他的学生。这样我们可以添加多个其他的学生,再用集合操作计算学生的分数,最高分,最低分,平均分等
.m文件实现不变。
在main函数中添加三个学生,添加到数组中,然后求平均分,最高,最低分,学生数量
运行打印结果
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。
下图来自苹果官网:
[img]
[/img]
如何使用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 "Student.h" @implementation Student @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; }
24.}
#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文件不改变就不管了
下面是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); 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。
发表评论
-
【IOS开发】全局变量之单例
2013-01-24 13:47 1073在开发中用单例存储项目中通用的全局变量是非常方便的,用法: ... -
Objective-C语法之Category的使用 .
2013-01-08 13:05 665论一个类设计的如何完美,都不可避免的会遇到没有预测到的需求,那 ... -
Objective-C语法之代码块(block)的使用 .
2013-01-08 12:55 691代码块本质上是和其他变量类似。不同的是,代码块存储的数据是一个 ... -
Objective-C语法之KVO的使用 .
2013-01-08 12:54 633简介: 上篇我们讲到了KVC,这篇我们学习KVO,全名为:Ke ... -
Objective-C语法之NSSet和NSMutableSet .
2013-01-06 10:09 979NSSet和NSMutableSet是无序的, 但是它保证数据 ... -
Objective-C语法之NSDictionary和NSMutableDictionary .
2013-01-06 10:09 646Java有Map,可以把数据以键值对的形式储存起来,取值的时候 ... -
Objective-C语法之NSArray和NSMutableArray .
2013-01-06 10:08 961Objective-C的数组比C++,Ja ... -
Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id)等 .
2013-01-06 10:08 910对象在运行时获取其类型的能力称为内省。内省可以有多种方法实现。 ... -
Objective-C语法之异常处理 .
2013-01-05 09:45 662Objective-C的异常比较像Java的异常处理,也有@f ... -
Objective-C语法之类和对象 .
2013-01-05 09:45 6241、类和方法 下图中是 ... -
Objective-C语法之字符串NSString .
2013-01-05 09:45 707Objective-C里核心的处理字符串的类就是NSStrin ... -
Objective-C语法之基本数据类型 .
2013-01-05 09:44 648本篇博客主要介绍基本数据类型的长度,基本数据类型在Oc的数组中 ... -
十分钟让你明白Objective-C的语法(和Java、C++的对比) .
2013-01-05 09:43 679很多想开发iOS,或者正在开发iOS的程序员以前都做过Java ...
相关推荐
3. Objective-C中的布尔类型 4. Objective-C中的null 5. 与C混合编写 6. 对象的初始化 7. Objective-C的description方法 8. Objective-C的异常处理 9. id类型 10. 类的继承 11. 动态判定与选择器 12. 类别Category ...
《Objective-C程序设计》(作者杨正洪、郑齐心、李建国)通过大量的实例系统地介绍了Objective-C语言的基本概念、语法规则、框架、类库及开发环境。读者在阅读本书后,可以掌握Objective-C语言的基本内容,并进行...
Objective-C Programming: The Big Nerd Ranch Guide (2nd Edition).epub (epub 格式) Want to write iOS apps or desktop Mac applications? This introduction to programming and the Objective-C language is ...
阅读本文档,您最好有一定的C语言、JAVA语言的基础。 文档内容: 1. GNUStep的安装 2. 类定义 3. Object-C中的布尔类型 4. Object-C中的null 5. 与C混合编写 6. 对象的初始化 7. Object-C的description方法 ...
1.&.批处理 1.(.KE4的简单运算 2.谓词 2.谓词 2.&.格式占位符
但需要注意的是,键值观察(KVO)是建立在键值编码(Key Value Coding,简称KVC)的基础上,也就是说任何你想使用KVO观察的属性必须符合键值编码。 这个demo主要以字符串、数组为例来学习键值编码和键值观察。 ...
技术关键词:Objective-C、iOS开发、Mac OS X、编程语言、面向对象编程、内存管理、自动引用计数(ARC)、协议(protocol)、类扩展(category)、键值观察(KVO)、键值编码(KVC)、Block、Delegate模式、多态性、...
objective-c下KVO-KVC模式的一个小小DEMO
IOS中Key-Value Coding (KVC)的使用详解(烟台杰瑞教育IOS培训部原创).docx编程资料
主要介绍了以实例讲解Objective-C中的KVO与KVC机制,即Key-Value-Observing与Key-Value-Coding,需要的朋友可以参考下
有点经验的都会觉得这种效果实现起来很简单但麻烦,常用的方法 Delegate、target-action、KVC 等组合使用,而且代码页面的粘合性也比很强。现在我们使用 ReactiveCocoa 来实现不但统一所有的消息,代码也更加的简洁...
KVO_App iOS Objective-C中KVC和KVO的基本实现
KVC(Key-Value Coding)是Objective-C中最强大最实用的功能之一。 通过例子可以帮你更好的理解它们。
与博客配套的KVC KVO代码,博客地址http://blog.csdn.net/liuyinghui523/article/details/77885198 简单的总结
--- 字典(由 KVC) 模型 ---> JSONString 模型 <--- JSONString 模型 <--> 模型(合并) 模型存储##用法字典到模型 + (instancetype)modelWithDictionary:(NSDictionary *)dictionary; + ...
KVC 与 KVO 使用姿势和原理解析
KVC编程思想:KVC运用了一个isa-swizzling技术。isa-swizzling就是类型混合指针机制。
KVC技术指南 ---ios官方
对kvo-kvc使用的说明。对于初学者有一定的帮助,对代码优化有一点好处。