NotificationCenter
A notification dispatch mechanism that enables the broadcast of information to registered observers.
Declaration
class NotificationCenterOverview
Callers register with a notification center to receive one or both of the following:
NSNotification objects, when working in Objective-C or with frameworks that only support NSNotification. Objects register with a notification center to receive notifications (NSNotification objects) using the addObserver(_:selector:name:object:) or addObserver(forName:object:queue:using:) methods, specifying a notification name and optionally a source object. When a caller adds itself as an observer, it specifies which notifications it should receive.
NotificationCenter.MainActorMessage and NotificationCenter.AsyncMessage instances for use with Swift code, providing strong typing, appropriate actor isolation, and a more idiomatic Swift experience. Callers register with the notification center using the various flavors of the
addObserver(of:for:using:)method, specifying either a message type or a convenience NotificationCenter.MessageIdentifier to identify the notification messages to receive. See Notification center messages for more information about this API.
Callers may add observers for many different notifications, or even the same notification name or message type as produced by different source objects.
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. On macOS, 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
Adding and removing notification observers
addObserver(forName:object:queue:using:)addObserver(_:selector:name:object:)removeObserver(_:name:object:)removeObserver(_:)