Copyright ã1994 by NeXT Computer, Inc.  All Rights Reserved.

NSNotification 

Inherits From:	NSObject

Conforms To:	NSCopying
NSObject (NSObject)

Declared In:	Foundation/NSNotification.h

Class Description

NSNotification objects provide a flexible way to transmit event information between objects.

Message passingÐinvoking a methodÐis the standard way to convey information between objects. However, this requires the object sending the message to know who the receiver is. At times this explicit binding of two objects is undesirableÐmost notably because it would tie two otherwise independent subsystems. For these instances, a looser broadcast model is introduced: An object posts a notification, which is dispatched to the appropriate receivers through a notification center.

An object may post an NSNotification object (referred to as a notification object or simply, a notification), which contains information about an object: the notification's name, its sender, and an optional dictionary containing other information. Other objects can register themselves as observers to receive notification objects when they are posted. When the event happens, the registered objects receive notifications about it. The object posting the NSNotification object, the object the notification is about, and the observer of the notification may all be different objects. 

An NSNotificationCenter object registers observers for events and notifies the observers if these events occur. An object may ask an NSNotificationCenter object (also known as a notification center) to observe an event regarding another object. If the event occurs, the posting object tells the notification center to notify its observers that this condition has occurred. The notification center then sends a notification to all observing objects. (See the class specification of NSNotificationCenter for more on posting notification objects.)

This notification model frees an object from concern about what objects may want to observe it. An object involved with an eventÐor another objectÐmay simply post a notification about that event without knowing what objectsÐif anyÐare observing the event. The notification center takes care of distributing notifications to registered observers. Another benefit of this model is to allow multiple objects to listen for notifications, an effect that might otherwise require explicitly setting up an array.

You instantiate a notification object directly by sending the notificationWithName:object: or notificationWithName:object:userInfo: messages to the NSNotification class object. You can also create notifications indirectly through the NSNotificationCenter class using the postNotificationName:object: and postNotificationName:object:userInfo: convenience methods. 

You can subclass NSNotification to contain information in addition to the notification name, sender, and dictionary.

NSNotification objects are immutable objects.

The NSNotification class adopts the NSCopying protocol, making it possible to treat notifications as context-independent values that can be copied and reused. You can put notifications in an array and send the copy message to that array, which recursively copies every item. This essentially allows clients to deal with notifications as first class values that can be copied by collections.

Creating Notification Objects

+ (NSNotification *)notificationWithName:(NSString *)aName
object:(id)anObject	Returns a notification object that associates the name aName with the object anObject.

+ (NSNotification *)notificationWithName:(NSString *)aName
object:(id)anObject	Returns a notification object that associates the name
userInfo:(NSDictionary *)userInfo		aName with the object anObject and the dictionary of arbitrary data userInfo. userInfo may be nil.

Querying a Notification Object

- (NSString *)name	Returns the name of the notification.

- (id)object	Returns the object (such as the sender) that's associated with this notification.

- (NSDictionary *)userInfo	Returns a dictionary object associated with this notification. Returns nil if there is no such object.