Let’s review the corresponding API:

So in viewControllerDidLoad I'll say: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"test"object:nil]; In the dealloc remove the selfCopy the code

The system has a notification center, which records the address of the observer. When there is a notification, the corresponding observer will be notified. If the observer is not there when the notification comes, the crash of the wild pointer will be caused theoretically. Before iOS9, developers had to remove it themselves, but look at the instructions in the apple documentation:

You can see that iOS9 doesn’t have to remove it, so why is that? What does the system do? Let’s take a wild guess, it removes it for us.

- (void)removeObserver:(id)observer
{
    NSLog(@ "====%@--%@ remove===" , [observer class],observer);
}
Copy the code

We override the system’s methods in the classification, we override the system’s methods, and then we test it in the Aviewcontroller

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notifycationMehod) name:@"testNotifycation" object:nil];
    // Do any additional setup after loading the view from its nib.
}
- (void)notifycationMehod{
    NSLog(@"TwoViewController receive notify");
}
- (void)dealloc{
    NSLog(@"AViewController dealloc");
}
// Do any additional setup after loading the view from its nib.
}
Copy the code

Then pop to see the print

The unsafe_unretain operation does not have a weak reference to the observer. The unsafe_unretain operation does not have a weak reference to the observer. The unsafe_unretain operation does not have a weak reference to the observer.