证明VC在dealloc时会自动删除监听

创建NSNotificationCenter的子类

@interface MyNotificationCenter : NSNotificationCenter

@end
@implementation MyNotificationCenter

+ (void)load {
    Method origin = class_getInstanceMethod([self class],@selector(removeObserver:));
    Method current = class_getInstanceMethod([self class],@selector(_removeObserver:));
    method_exchangeImplementations(origin, current);
}

- (void)_removeObserver:(id)observer {
    NSLog(@"调用移除通知方法: %@",observer);
    //[self _removeObserver:observer];
}

@end

当VC被释放时,即使我们不在dealloc中写

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

我们的Log依然会触发,且可以在log中找到vc对象

你可能感兴趣的:(证明VC在dealloc时会自动删除监听)