关于KVC、KVO
KVC/KVO
--------------------KVC--键值编码-------------------
作用:
通过字符串来描述对象的属性间接修改对象的属性
Student *stu=[[Student alloc]init];//实例化Student 对象
Student 成员变量:name ,age, book
Book 成员变量 price
1 修改price属性
[stu setValue @“10.3”forKey:@“age”]
1.0获取属性值
[stu valueForkey:@"age"];
2 批量修改属性
[stu setValuesWithDictionary:@ {@"age":@"22",@"name":@"傻强"}];
2.0获取批量值(返回值字典)
NSArray *array= [stu dictionaryWithValues:@[@"age",@"name"]];
3 修改属性对象的属性
stu.book=[[BOOK alloc]init];
[stu.book setValue:@10.7 ForKey:@"price"];
[stu setValue:@"10.7" ForKeyPath:@"book.price"];
4 直接获取数组中所有对象的name属性值(name为对象的属性)
Student *stu1=[[Student alloc]init];
Student *stu2=[[Student alloc]init];
Student *stu3=[[Student alloc]init];
NSArray *array=@[stu1,stu2,stu3];
NSArray *nameArray=[array valueForKeyPath:@"name"];
5 直接获取数组中所有对象的price属性值的和(price为对象的属性对象book的属性)
stu1.book=[[BOOK alloc]init];
stu1.book.price=15;
stu2.book=[[BOOK alloc]init];
stu2.book.price=20;
stu3.book=[[BOOK alloc]init];
stu3.book.price=25;
NSArray *sumPrice=@[stu1,stu2,stu3];
id sum=[sumPrice valueForKeyPath:book.@sum.price];
--------------------KVO--键值监听-------------------
作用:
监听对象的属性的变化
下面以用Student对象来监听BOOK对象的price属性值的变化为例
------------------BOOK.m------------------------
BOOK *book=[[BOOK alloc]init];
book.price=10;//price原来的值
Student *stu=[[Student alloc]init];
//添加监听器
[book addObserver:stu forKeyPath:@"price" options:NSKeyValueObserverOptionNew |NSKeyValueObserverOptionOld context:nil];
//book:添加监听器者,stu:被添加监听器者,price:监听对象的什么属性,options:监听类型 context:上下文,这里暂时不需要,可填写为空;
book.price=50;//price变化后的值
//移出监听器
[book removeObserver:stu forKeyPath :@"price"]
------------------Student.m------------------
//当所监听的属性发生改变的时候,会调用这个方法
- (void)observerValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
//keyPath:被监听的属性,这里为name。
//object:哪个对象的属性,这里为book。
//change :属性值变化为什么(字典),这里为old=10,new=50.
//context:上下文为nil。
}
关于KVC、KVO的更多相关文章
- KVC/KVO原理详解及编程指南
一.简介 1.KVC简介 2.KVO简介 二.KVC相关技术 1.Key和Key Path 2.点语法和KVC 3.一对多关系(To-Many)中的集合访问器方法 4.键值验证(Key-Value V ...
- 【转】 KVC/KVO原理详解及编程指南
原文地址:http://blog.csdn.net/wzzvictory/article/details/9674431 前言: 1.本文基本不讲KVC/KVO的用法,只结合网上的资料说说对这种技术的 ...
- kvc/kvo复习
kvc/kvo复习 1 小问题 '[<XMGPerson 0x7fb8a8f30220> setValue:forUndefinedKey:]: this XMGPerson * pers ...
- 转:KVC/KVO原理详解及编程指南
作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/9674431 转载请注明出处 如果觉得文章对你有所帮助,请通过留言或 ...
- 阶段性总结⓵触摸事件&手势识别⓶Quartz2D绘图⓷CALayer图层⓸CAAnimation⓹UIDynamic UI动力学⓺KVC&KVO
知识点复习 1. 触摸事件&手势识别 1> 4个触摸事件,针对视图的 2> 6个手势识别(除了用代码添加,也可以用Storyboard添加) 附加在某一个特定视图上的, ...
- IOS开发之KVC KVO KVB
KVC(Key Value Coding) KVO(Key Value Observing) KVB(Key Value Binding) KVO是Cocoa的一个重要机制,他提供了观察某一属性变化的 ...
- KVC & KVO
KVC和KVO看上去又是两个挺牛的单词简写,KVC是Key-Value Coding的简写,是键值编码的意思.KVO是Key-Value Observing的简写,是键值观察的意思.那么我们能拿KV ...
- KVC&&&KVO
KVC 什么是KVC --->What KVC指的就是NSKeyValueCoding非正式协议. KVC是一种间接地访问对象的属性的机制. 这种间接表现在通过字符串来标识属性,而不是通过调用存 ...
- 04 KVC|KVO|Delegate|NSNotification区别
一. iOS 中KVC.KVO.NSNotification.delegate 在实际的编程中运用的非常多,掌握好他们的运行原理和使用场合对于我们程序的开发将会带来事办工倍的效果: 二. KVC ...
- 深入理解 KVC\KVO 实现机制 — KVC
KVC和KVO都属于键值编程而且底层实现机制都是isa-swizzing,所以本来想放在一起讲的.但是篇幅有限所以就分成了两篇博文 KVO实现机制传送门 KVC概述 KVC是Key Value Cod ...
随机推荐
- php 调用系统命令
system 与 exec 两者区别与联系:都会返回最后一行,命令执行成功的return返回值, 区别:system直接将输出内容echo出来,而exec将每一行输出内容保存到数组$output里. ...
- MySQL Python教程(4)
Class cursor.MySQLCursorBuffered 该类从Class cursor.MySQLCursorBuffered继承,如果需要,可以在执行完SQL语句后自动缓冲结果集合.imp ...
- Android隐藏标题栏和状态栏
一.隐藏标题栏 //隐藏标题栏 this.requestWindowFeature(Window.FEATURE_NO_TITLE); 二.隐藏状态栏 //隐藏状态栏 this.getWindow() ...
- 如何将代码托管到GitHub上
注册账号什么的直接省略吧...GitHub传送门:https://github.com/ 首先就是创建repository. 接着就是填写相关的信息了 点击Create repository,创建成功 ...
- poj3984
定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...
- 5.1---二进制数插入(CC150)
public class Solution { public static int binInsert(int n, int m, int i, int j) { // write code here ...
- XAMPP端口占用启动不了
skype默认会占用80和443端口 如果在apache之前启动skype,apache就会启动不了了!! 解决办法很简单: 1. 先启动apache再启动skype,这样skype就会换其他的端口监 ...
- Asp.net MVC网站的基本结构
Asp.net MVC网站的基本结构:Controller->IBLL(定义业务接口)->BLL->DA(调用Utility)->数据库 ,其中A->B表示A调用B 1. ...
- Discovering versions from the identity service failed when creating the password plugin.
If you are behind the proxy, then you have to set no_proxy environmental variable for your localhost ...
- 【GoLang】GoLang 遍历 map、slice、array方法
代码示例: map1 := make(map[string]string) map1["a"] = "AAA" map1["b"] = &q ...