首页 > 代码库 > ios开发--KVO浅析
ios开发--KVO浅析
目标:监听NSMutableArray对象中增加了什么
代码如下:
C代码
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.dataArray = [NSMutableArray arrayWithObject:@"1"];
- [self addObserver:self forKeyPath:@"dataArray" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
- }
- (void)viewDidLoad{ [super viewDidLoad]; self.dataArray = [NSMutableArray arrayWithObject:@"1"]; [self addObserver:self forKeyPath:@"dataArray" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL]; }
C代码
- - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
- {
- NSLog(@"%@", keyPath);
- NSLog(@"%@", object);
- NSLog(@"%@", change);
- }
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ NSLog(@"%@", keyPath); NSLog(@"%@", object); NSLog(@"%@", change);}
C代码
- - (IBAction)add:(id)sender
- {
- NSArray *addData = [NSArray arrayWithObjects:@"11", @"12", @"13", nil];
- [self.dataArray addObjectsFromArray:addData];
- self.dataArray = [NSMutableArray arrayWithObject:@"2"];
- }
- (IBAction)add:(id)sender{ NSArray *addData = http://www.mamicode.com/[NSArray arrayWithObjects:@"11", @"12", @"13", nil]; [self.dataArray addObjectsFromArray:addData]; self.dataArray = [NSMutableArray arrayWithObject:@"2"];}
输入日志:
C代码
- 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
- );
- }
2013-01-15 16:05:10.120 KVOTest[2199:907] dataArray2013-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的变化是不可行的
但,我们可以通过此方式来做控件属性的变动。如下:
C代码
- - (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变化
- }
- (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变化}
C代码
- - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
- {
- NSLog(@"%@", keyPath);
- NSLog(@"%@", object);
- NSLog(@"%@", change);
- }
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ NSLog(@"%@", keyPath); NSLog(@"%@", object); NSLog(@"%@", change);}
C代码
- - (IBAction)add:(id)sender
- {
- [self.personObject.bankInstance setAccountBalance:2111];
- }
- (IBAction)add:(id)sender{ [self.personObject.bankInstance setAccountBalance:2111];}
输出日志:
C代码
- 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;
- }
2013-01-15 16:05:10.111 KVOTest[2199:907] bankInstance.accountBalance2013-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浅析
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。