Class

NotificationCenter

An NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a program. An NSNotificationCenter object is essentially a notification dispatch table.

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. Each invocation of this method specifies a set of notifications. Therefore, objects may register as observers of different notification sets by calling these methods several times.

Each running Cocoa program has a default notification center. You typically don’t create your own. An NSNotificationCenter object 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 an instance of DistributedNotificationCenter.

Symbols

Getting the Notification Center

class var `default`: NotificationCenter

Returns the process’s default notification center.

Managing Notification Observers

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

Adds an entry to the receiver’s dispatch table with a notification queue and a block to add to the queue, and optional criteria: notification name and sender.

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

Adds an entry to the receiver’s dispatch table with an observer, a notification selector and optional criteria: notification name and sender.

func removeObserver(Any)

Removes all the entries specifying a given observer from the receiver’s dispatch table.

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

Removes matching entries from the receiver’s dispatch table.

Posting Notifications

func post(Notification)

Posts a given notification to the receiver.

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

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

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 receiver.

Relationships

Inherits From

Conforms To