Ios observeValueForKeyPath:发送到解除分配实例的消息
我有一个简单的UIView孩子,我很困惑为什么会出现这个错误(导致应用程序崩溃):Ios observeValueForKeyPath:发送到解除分配实例的消息,ios,objective-c,automatic-ref-counting,nsnotificationcenter,Ios,Objective C,Automatic Ref Counting,Nsnotificationcenter,我有一个简单的UIView孩子,我很困惑为什么会出现这个错误(导致应用程序崩溃): [VideoView observeValueForKeyPath:of对象:更改:上下文:]:消息发送到解除分配的实例0x13009b670 以下是VideoView课程: @implementation VideoView - (id)init { self = [super init]; if (self) { [[NSNotificationCenter defaultC
[VideoView observeValueForKeyPath:of对象:更改:上下文:]:消息发送到解除分配的实例0x13009b670
以下是VideoView
课程:
@implementation VideoView
- (id)init {
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(stopVideo)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
//.. stopVideo method
@end
我的dealloc
是否确保从未向已解除分配的实例发送通知?我还能如何防止这种情况发生
iOS 9,ARC启用你把事情搞混了。该错误不是由
NSNotificationCenter
通知引起的。在您正在使用的代码中的某个地方,当对象被解除分配时,您没有删除该观察者,这就是发生崩溃的原因。您将事情搞混了。该错误不是由NSNotificationCenter
通知引起的。在您正在使用的代码中的某个地方,当对象被解除分配时,您不会删除该观察者,这就是发生崩溃的原因。Ahh正确,添加[self.player removeObserver:self forKeyPath:@“currentItem.status”]代码>解决了问题。但必须注意例外情况:啊,对,添加[self.player removeObserver:self forKeyPath:@“currentItem.status”]代码>解决了问题。但必须小心例外情况: