NSNotification objects encapsulate information so that it can be broadcast to other objects by an NSNotificationCenter object. An NSNotification object (referred to as a notification) contains a name, an object, and an optional dictionary. The name is a tag identifying the notification. The object is any object that the poster of the notification wants to send to observers of that notification (typically, it is the object that posted the notification). The dictionary stores other related objects, if any. NSNotification objects are immutable objects.
- iOS 2.0+
- macOS 10.0+
- tvOS 9.0+
- watchOS 2.0+
You can create a notification object with the class methods
notification. However, you don’t usually create your own notifications directly. The NSNotificationCenter methods
post(name: allow you to conveniently post a notification without creating it first.
The objects of a notification are compared using pointer equality for local notifications. Distributed notifications use strings as their objects, and those strings are compared using
is because pointer equality doesn’t make sense across process boundaries.
You can subclass
NSNotification to contain information in addition to the notification name, object, and dictionary. This extra data must be agreed upon between notifiers and observers.
NSNotification is a class cluster with no instance variables. As such, you must subclass
NSNotification and override the primitive methods
user. You can choose any designated initializer you like, but be sure that your initializer does not call
NSNotification is not meant to be instantiated directly, and its
init method raises an exception.