Instance Method

addObserver(forName:object:queue:using:)

Adds an entry to the notification center's dispatch table that includes a notification queue and a block to add to the queue, and an optional notification name and sender.

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 for which to register the observer; that is, only notifications with this name are used to add the block to the operation queue.

If you pass nil, the notification center doesn’t use a notification’s name to decide whether to add the block to the operation queue.

obj

The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer.

If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.

queue

The operation queue to which block should be added.

If you pass nil, the block is run synchronously on the posting thread.

block

The block to be executed when the notification is received.

The block is copied by the notification center and (the copy) held until the observer registration is removed.

The block takes one argument:

notification

The notification.

Return Value

An opaque object to act as the observer.

Discussion

If a given notification triggers more than one observer block, the blocks may all be executed concurrently with respect to one another (but on their given 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)")
}

To unregister observations, you pass the object returned by this method to removeObserver(_:). You must invoke removeObserver(_:) or removeObserver(_:name:object:) before any object specified by addObserver(forName:object:queue:using:) is deallocated.

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

Another common pattern 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's dispatch table with an observer and a notification selector, and an optional notification name and sender.

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 a given observer from the notification center's dispatch table.