iOS开发中的bug

KVO常见错误

2016-04-15  本文已影响3286人  天空没有太阳

KVO的常见错误

*** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <SubViewController 0x7ff158707f80> for the key path "age" from <SubViewController 0x7ff158707f80> because it is not registered as an observer.'

修改前代码

// 添加观察者
    Person *person = [[Person alloc] init];
    person.name = @"hello";
    person.age = 23;
    self.subPersonKVO = person;
    [self.subPersonKVO addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"SubViewController"];
// 移除观察者 在dealloc中
    [self removeObserver:self forKeyPath:@"age" context:@"SubViewController"];

=============
解决方案

// 移除观察者 在dealloc中
    [self.subPersonKVO removeObserver:self forKeyPath:@"age" context:@"SubViewController"];

self -> self.subPersonKVO 关键点就在这里,观察谁,谁就应该移除

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<SubViewController: 0x7fc273d13450>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: age

解决方案:添加该方法

`- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    NSLog(@"keyPath=%@", keyPath);
    self.subLabelKVO.text = [NSString stringWithFormat:@"%@现在的年龄是: %zd", self.subPersonKVO.name, self.subPersonKVO.age];
}
*** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <SubViewController 0x7fc175001d60> for the key path "age" from <Person 0x7fc172da5be0> because it is not registered as an observer.'

修改前

`- (void) kvo {
Person *person = [[Person alloc] init];
    person.name = @"hello";
    person.age = 23;
    self.subPersonKVO = person;
    [self.subPersonKVO addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"SubViewController"];
}
`- (void)dealloc {
    [self.subPersonKVO removeObserver:self forKeyPath:@"age" context:nil];
}

======
解决方案:context 统一

`- (void)dealloc {
    [self.subPersonKVO removeObserver:self forKeyPath:@"age" context:@"SubViewController"];
}

一般来说context都是nil,但是个人还是觉得区别开来为好
项目地址:https://github.com/wosta/OLKVO-KVC

上一篇 下一篇

猜你喜欢

热点阅读