关于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 ...
随机推荐
- 使用JavaScript在项目前台开发的58种常用小技巧
oncontextmenu="return false" :禁止右键 onselectstart="return false" : 禁止选取 onpaste = ...
- Laravel5.1-Eloquent ORM:起步
概述 有很多朋友问,MCV的M是在哪里介绍的,这里就是介绍M的地方了.Laravel有一个强大的数据库ORM Eloquent,它的原理是每张数据表对应一个Model,对Model的操作就对应数据库的 ...
- Kali Linux渗透基础知识整理(二)漏洞扫描
Kali Linux渗透基础知识整理系列文章回顾 漏洞扫描 网络流量 Nmap Hping3 Nessus whatweb DirBuster joomscan WPScan 网络流量 网络流量就是网 ...
- 2016-03-12 Leanning Plan
1,Python 2,Java 3,Html+Css 4,PHP 5,Crawl 6,WetChat Platform
- ModelAndView的介绍
ModelAndView的构造方法有7个.但是它们都是相通的.这里使用无参构造函数来举例说明如何构造ModelAndView实例. ModelAndView类别就如其名称所示,是代表了MVC Web程 ...
- dtw算法
dtw路径与线性变换路径对比 转自:http://baike.baidu.com/link?url=z4gFUEplOyqpgboea6My0mZP ...
- Resizing the disk space on Ubuntu Server VMs running on VMware ESXi 5
from: http://www.joomlaworks.net/blog/item/168-resizing-the-disk-space-on-ubuntu-server-vms-running- ...
- struts2 配置 struts.xml 提示
1.这个提示通常是在 连网络的时候才可以看到 2.当没有网路的时候我们该如何配置呢? window -->preferences -->xml catelog -->user.... ...
- ios awakeFromNib 和 initWithCoder:
During the instantiation process, each object in the archive is unarchived and then initialized with ...
- MongoDB 数据库管理(不定时更新)
之前的几篇文章大致说了副本集的搭建.副本集的管理,现在说下MongoDB数据库的管理.数据库管理包括:备份.还原.导入.导出.服务器管理等. 一:查看服务器状态,查看命令行参数.db.serverSt ...