Class

NotificationCenter

A notification dispatch mechanism that enables the broadcast of information to registered observers.

Overview

Objects register with a notification center to receive notifications (NSNotification objects) using the addObserver(_:selector:name:object:) or addObserver(forName:object:queue:using:) methods. When an object adds itself as an observer, it specifies which notifications it should receive. An object may therefore call this method several times in order to register itself as an observer for several different notifications.

Each running app has a default notification center, and you can create new notification centers to organize communications in particular contexts.

A notification center can deliver notifications only within a single program; if you want to post a notification to other processes or receive notifications from other processes, use DistributedNotificationCenter instead.

Topics

Getting the Default Notification Center

class var `default`: NotificationCenter

The app’s default notification center.

Adding and Removing Notification Observers

func addObserver(forName: NSNotification.Name?, object: Any?, queue: OperationQueue?, using: (Notification) -> Void)

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.

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.

Posting Notifications

func post(Notification)

Posts a given notification to the notification center.

func post(name: NSNotification.Name, object: Any?, userInfo: [AnyHashable : Any]? = nil)

Creates a notification with a given name, sender, and information and posts it to the notification center.

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

Creates a notification with a given name and sender and posts it to the notification center.

Relationships

Inherits From

Conforms To

See Also

Notifications

struct Notification

A container for information broadcast through a notification center to all registered observers.

class NotificationQueue

A notification center buffer.