Class

NSNotification

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

Overview

A notification contains a name, an object, and an optional dictionary, and is broadcast to by instances of NSNotificationCenter or NSDistributedNotificationCenter. 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, the object posting the notification). The dictionary stores other related objects, if any. NSNotification objects are immutable.

You don’t usually create your own notifications directly, but instead call the NSNotificationCenter methods postNotificationName:object: and postNotificationName:object:userInfo:.

Object Comparison

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 isEqual:, because pointer equality doesn’t make sense across process boundaries.

Creating Subclasses

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.

NSNotificationCenter is a class cluster with no instance variables. As such, you must subclass NSNotification and override the primitive methods name, object, and userInfo. You can choose any designated initializer you like, but be sure that your initializer does not call init on super (NSNotification is not meant to be instantiated directly, and its init method raises an exception).

Topics

Creating Notifications

init

Initializes an empty notification.

initWithCoder:

Initializes a notification with the data from an unarchiver.

notificationWithName:object:

Returns a new notification object with a specified name and object.

notificationWithName:object:userInfo:

Returns a notification object with a specified name, object, and user information.

initWithName:object:userInfo:

Initializes a notification with a specified name, object, and user information.

NSNotificationName

The type used for the name of a notification.

Getting Notification Information

name

The name of the notification.

object

The object associated with the notification.

userInfo

The user information dictionary associated with the notification.

Relationships

Inherits From

Conforms To

See Also

Notifications

NSNotificationCenter

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

NSNotificationQueue

A notification center buffer.