addObserver(_:selector:name:object:)
Adds an entry to the notification center to call the provided selector with the notification.
Declaration
func addObserver(_ observer: Any, selector aSelector: Selector, name aName: NSNotification.Name?, object anObject: Any?)Parameters
- observer:
An object to register as an observer.
- aSelector:
A selector that specifies the message the receiver sends
observerto alert it to the notification posting. The method thataSelectorspecifies must have one and only one argument (an instance ofNSNotification). - aName:
The name of the notification to register for delivery to the observer. Specify a notification name to deliver only entries with this notification name.
When
nil, the sender doesn’t use notification names as criteria for the delivery. - anObject:
The object that sends notifications to the observer. Specify a notification sender to deliver only notifications from this sender.
When
nil, the notification center doesn’t use sender names as criteria for delivery.
Discussion
Unregister an observer to stop receiving notifications.
To unregister an observer, use NotificationCenter/removeObserver(_:) or removeObserver(_:name:object:) with the most specific detail possible. For example, if you used a name and object to register the observer, use the name and object to remove it.
If your app targets iOS 9.0 and later or macOS 10.11 and later, you do not need to unregister an observer that you created with this function. If you forget or are unable to remove an observer, the system cleans up the next time it would have posted to it.