KVO崩溃原因

实现kvo的时候,如果没有实现

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

if ([keyPath isEqualToString:@"contentOffset"]) {

NSValue *y = [change objectForKey:@"new"];

if ([y isKindOfClass:[NSValue class]]) {

if (y.CGPointValue.y<=0) {

} else {

}

}

}

else {

[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

}

}

方法,会因为找不到方法实现而崩溃,


在dealloc方法里,如果没移除监听,那么obsever被释放后,被观察对象调用observer方法会访问野指针,造成崩溃。也就是说被观察对象会在observer释放后释放。

你可能感兴趣的:(KVO崩溃原因)