Instance Method

addObserver(forName:object:queue:using:)

Adds an entry to the notification center to receive notifications that passed to the provided block.

Declaration

func addObserver(forName name: NSNotification.Name?, object obj: Any?, queue: OperationQueue?, using block: @escaping (Notification) -> Void) -> NSObjectProtocol

Parameters

name

The name of the notification to register for delivery to the observer block. Specify a notification name to deliver only entries with this notification name.

When nil, the sender doesn’t use notification names as criteria for delivery.

obj

The object that sends notifications to the observer block. Specify a sender to deliver only notifications from this sender.

When nil, the notification center doesn’t use the sender as criteria for the delivery.

queue

The operation queue where the block runs.

When nil, the block runs synchronously on the posting thread.

block

The block that executes when receiving a notification.

The notification center copies the block. The notification center strongly holds the copied block until you remove the observer registration.

The block takes one argument: the notification.

Return Value

An opaque object to act as the observer. Notification center strongly holds this return value until you remove the observer registration.

Discussion

If a notification triggers more than one observer block, the blocks can all execute concurrently (but on their queue or on the current thread).

The following example shows how you can register to receive locale change notifications.

let center = NSNotificationCenter.defaultCenter()
let mainQueue = NSOperationQueue.mainQueue()
self.localeChangeObserver = center.addObserverForName(NSCurrentLocaleDidChangeNotification, object: nil, queue: mainQueue) { (note) in
    print("The user's locale changed to: \(NSLocale.currentLocale().localeIdentifier)")
}

Unregister an observer to stop receiving notifications. To unregister an observer, use 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.

You must invoke removeObserver(_:) or removeObserver(_:name:object:) before the system deallocates any object that addObserver(forName:object:queue:using:) specifies.

let center = NSNotificationCenter.defaultCenter()
center.removeObserver(self.localeChangeObserver)

Another common practice is to create a one-time notification by removing the observer from within the observation block, as in the following example.

let center = NSNotificationCenter.defaultCenter()
let mainQueue = NSOperationQueue.mainQueue()
var token: NSObjectProtocol?
token = center.addObserverForName("OneTimeNotification", object: nil, queue: mainQueue) { (note) in
    print("Received the notification!")
    center.removeObserver(token!)
}

See Also

Adding and Removing Notification Observers

func addObserver(Any, selector: Selector, name: NSNotification.Name?, object: Any?)

Adds an entry to the notification center to call the provided selector with the notification.

func removeObserver(Any, name: NSNotification.Name?, object: Any?)

Removes matching entries from the notification center's dispatch table.

func removeObserver(Any)

Removes all entries specifying an observer from the notification center's dispatch table.