Is there an easy-to-grock pattern how to send a NSNotification (Objective C) | Notification (in Swift) and how to receive one? Code snippet? The docs write like 150 pages on the topic. Would like to see a quick example.
First, register an observer for a notification with: addObserver(_:selector:name:object:) Then, post a notification with post(name:object:userInfo:) … … after which your selector is called. And don't forget to remove the observer with removeObserver()
A notification dispatch mechanism that enables the broadcast of information to registered observers.
addObserver(forName:object:queue:using:) Adds an entry to the notification center to receive notifications that passed to the provided block.
Send a notification:
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyCacheUpdatedNotification" object:self];
Receive it:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cacheUpdated:) name:@"MyCacheUpdatedNotification" object:nil];
Act on it:
- (void)cacheUpdated:(NSNotification *)notification { [self load]; }
And dispose of it:
[[NSNotificationCenter defaultCenter] removeObserver:self];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With