目标:监听NSMutableArray对象中增加了什么

代码如下:

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. self.dataArray = [NSMutableArray arrayWithObject:@"1"];
  5. [self addObserver:self forKeyPath:@"dataArray" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
  6. }
- (void)viewDidLoad
{
[super viewDidLoad]; self.dataArray = [NSMutableArray arrayWithObject:@"1"];
[self addObserver:self forKeyPath:@"dataArray" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL]; }
  1. - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  2. {
  3. NSLog(@"%@", keyPath);
  4. NSLog(@"%@", object);
  5. NSLog(@"%@", change);
  6. }
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%@", keyPath);
NSLog(@"%@", object);
NSLog(@"%@", change);
}
  1. - (IBAction)add:(id)sender
  2. {
  3. NSArray *addData = [NSArray arrayWithObjects:@"11", @"12", @"13", nil];
  4. [self.dataArray addObjectsFromArray:addData];
  5. self.dataArray = [NSMutableArray arrayWithObject:@"2"];
  6. }
- (IBAction)add:(id)sender
{
NSArray *addData = [NSArray arrayWithObjects:@"11", @"12", @"13", nil];
[self.dataArray addObjectsFromArray:addData]; self.dataArray = [NSMutableArray arrayWithObject:@"2"];
}

输入日志:

  1. 2013-01-15 16:05:10.120 KVOTest[2199:907] dataArray
  2. 2013-01-15 16:05:10.121 KVOTest[2199:907] <ZZTViewController: 0x20846590>
  3. 2013-01-15 16:05:10.123 KVOTest[2199:907] {
  4. kind = 1;
  5. new =     (
  6. 2
  7. );
  8. old =     (
  9. 1,
  10. 11,
  11. 12,
  12. 13
  13. );
  14. }
2013-01-15 16:05:10.120 KVOTest[2199:907] dataArray
2013-01-15 16:05:10.121 KVOTest[2199:907] <ZZTViewController: 0x20846590>
2013-01-15 16:05:10.123 KVOTest[2199:907] {
kind = 1;
new = (
2
);
old = (
1,
11,
12,
13
);
}

经过测试得如下结论:kvo监听的是对象指针的变动,NSString、int、float等对象的变动(abc = @"123"、abc = 12、abc = 12.2)皆为指针的变动,所以通过此方式来捕捉array的变化是不可行的

但,我们可以通过此方式来做控件属性的变动。如下:

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. self.personObject = [PersonObject personObjectWithBankInstance:[BankObject bankObjectWithAccountBalance:10]];
  5. [self.personObject addObserver:self forKeyPath:@"bankInstance.accountBalance" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];    // 此处注意是监听personObject对象下的bankInstance的accountBalance变化
  6. }
- (void)viewDidLoad
{
[super viewDidLoad]; self.personObject = [PersonObject personObjectWithBankInstance:[BankObject bankObjectWithAccountBalance:10]]; [self.personObject addObserver:self forKeyPath:@"bankInstance.accountBalance" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL]; // 此处注意是监听personObject对象下的bankInstance的accountBalance变化
}
  1. - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  2. {
  3. NSLog(@"%@", keyPath);
  4. NSLog(@"%@", object);
  5. NSLog(@"%@", change);
  6. }
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%@", keyPath);
NSLog(@"%@", object);
NSLog(@"%@", change);
}
  1. - (IBAction)add:(id)sender
  2. {
  3. [self.personObject.bankInstance setAccountBalance:2111];
  4. }
- (IBAction)add:(id)sender
{
[self.personObject.bankInstance setAccountBalance:2111];
}

输出日志:

  1. 2013-01-15 16:05:10.111 KVOTest[2199:907] bankInstance.accountBalance
  2. 2013-01-15 16:05:10.116 KVOTest[2199:907] <PersonObject: 0x20856180>
  3. 2013-01-15 16:05:10.118 KVOTest[2199:907] {
  4. kind = 1;
  5. new = 2111;
  6. old = 10;
  7. }
2013-01-15 16:05:10.111 KVOTest[2199:907] bankInstance.accountBalance
2013-01-15 16:05:10.116 KVOTest[2199:907] <PersonObject: 0x20856180>
2013-01-15 16:05:10.118 KVOTest[2199:907] {
kind = 1;
new = 2111;
old = 10;
}

如有问题,请留言共同探讨。

ios开发--KVO浅析的更多相关文章

  1. iOS开发-KVO的奥秘

    转自:http://www.jianshu.com/p/742b4b248da9 序言 在iOS开发中,苹果提供了许多机制给我们进行回调.KVO(key-value-observing)是一种十分有趣 ...

  2. iOS开发——实用篇&KVO与KVC详解

    KVO与KVC详解 由于ObjC主要基于Smalltalk进行设计,因此它有很多类似于Ruby.Python的动态特性,例如动态类型.动态加载.动态绑定等.今天我们着重介绍ObjC中的键值编码(KVC ...

  3. iOS开发——OC篇&消息传递机制(KVO/NOtification/Block/代理/Target-Action)

     iOS开发中消息传递机制(KVO/NOtification/Block/代理/Target-Action)   今晚看到了一篇好的文章,所以就搬过来了,方便自己以后学习 虽然这一期的主题是关于Fou ...

  4. iOS开发中KVC、KVO简介

    在iOS开发中,KVC和KVO是经常被用到的.可以使用KVC对对象的属性赋值和取得对象的属性值,可以使用KVO监听对象属性值的变化.简单介绍一下KVC和KVO. 一:键值编码(KVC) KVC,全称 ...

  5. iOS开发frame, contentSize, contentOffset, contentInset 区别联系浅析

    1. 概述 iOS开发中,必然会涉及到布局相关问题,frame,bounds,contenSize,contentOffset,contentInset这几个布局相关概念让许多初学者感到困惑.虽然初步 ...

  6. iOS开发系列文章(持续更新……)

    iOS开发系列的文章,内容循序渐进,包含C语言.ObjC.iOS开发以及日后要写的游戏开发和Swift编程几部分内容.文章会持续更新,希望大家多多关注,如果文章对你有帮助请点赞支持,多谢! 为了方便大 ...

  7. iOS开发系列--Swift进阶

    概述 上一篇文章<iOS开发系列--Swift语言>中对Swift的语法特点以及它和C.ObjC等其他语言的用法区别进行了介绍.当然,这只是Swift的入门基础,但是仅仅了解这些对于使用S ...

  8. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  9. IOS开发基础知识--碎片42

    1:报thread 1:exc_bad_access(code=1,address=0x70********) 闪退 这种错误通常是内存管理的问题,一般是访问了已经释放的对象导致的,可以开启僵尸对象( ...

随机推荐

  1. 学习总结 html 表格标签的使用

    表格: <table></table>表格 width:宽度.可以用像素或百分比表示. 常用960像素. border:边框,常用值为0. cellpadding:内容跟边框的 ...

  2. .net Url重写

    详细说明及下载dll源码路径: http://msdn.microsoft.com/zh-cn/library/ms972974.aspx 顺带上本人写的一个小例子:http://files.cnbl ...

  3. LoadRunner性能测试指挥中心Controller 《第四篇》

    一.设计场景 1.设计性能测试场景 Controller有两个视图:设计(Design)视图和运行(Run)视图.启动Controller,在Controller主界面里,我们可以看到这两个视图. 1 ...

  4. 【MySQL】MySQL事务回滚脚本

    MySQL自己的 mysqlbinlog | mysql 回滚不好用,自己写个简单脚本试试: 想法是用mysqlbinlog把需要回滚的事务区域从mysql-bin.file中找到,然后通过脚本再插入 ...

  5. Unity Js与C#脚本通信

    将.js文件放到Standard Assets目录下,否则无法编译通过 CS_test.cs : using UnityEngine; using System.Collections;   publ ...

  6. 关于hbase的read操作的深入研究 region到storefile过程

    这里面说的read既包括get,也包括scan,实际底层来看这两个操作也是一样的.我们将要讨论的是,当我们从一张表读取数据的时候hbase到底是怎么处理的.分二种情况来看,第一种就是表刚创建,所有pu ...

  7. 如何避免遭受HTTS中间人攻击

    先前为大家说明了如何对App的HTTPS通讯进行中间人攻击,听起来很吓人吧-表示若是使用手机的网银或购物等App,便有可能暴露在风险之中. 会发生HTTPS遭受拦截的主要原因是客户端的App未对服务器 ...

  8. 著名的二分查找的BUG

    int binarySearch(int a[], int key, int length) { int low = 0; int high = length - 1; while (low < ...

  9. 刚开始学IOS遇到的类和方法

    框架:Core FoundationCFGetRetainCount. 类:NSRunLoop.NSAutoreleasePool.NSStringFormClass.UIApplicationMai ...

  10. 一些CSS"bug"

    1.img三像像素问题 解决办法:img{display:block;} or img{vertical-align:middle;} 问题原因:img是行内元素,默认的垂直对齐方式 baseline ...