PATH  Documentation > Mac OS X > Foundation Reference: Java



Table of Contents

NSNotification


Inherits from:
NSObject
Package:
com.apple.yellow.foundation


Class Description


NSNotification objects encapsulate information so that it can be broadcast to other objects by an NSNotificationCenter object.


Notifications and their Rationale


The standard way to pass information between objects is message passing-one object invokes the method of another object. However, message passing requires that the object sending the message know who the receiver is and what messages it responds to. At times, this tight coupling of two objects is undesirable-most notably because it would join together two otherwise independent subsystems. For these cases, a broadcast model is introduced: An object posts a notification, which is dispatched to the appropriate observers through an NSNotificationCenter object, or simply notification center.

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.

Any object may post a notification. Other objects can register themselves as observers to receive notifications when they are posted. The object posting the notification, the object included in the notification, and the observer of the notification may all be different objects or the same object. Objects that post notifications need not know anything about the observers. On the other hand, observers need to know at least the notification name and keys to the dictionary if provided.

NSNotification objects are immutable objects.


Notification Centers


The notification center manages the sending and receiving of notifications. When an object wants to receive a certain notification, it registers itself with the notification center. When an object has a notification to send, it sends it to the notification center. When the notification center receives a notification, it passes that notification along to all objects registered to receive it. (See the NSNotificationCenter class specification for more on posting notifications.)

This notification model frees an object from concern about what objects it should send information to. Any object may simply post a notification without knowing what objects-if any-are receiving the notification. However, objects receiving notifications do need to know at least the notification name if not the type of information the notification contains. The notification center takes care of broadcasting notifications to registered observers. Another benefit of this model is to allow multiple objects to listen for notifications, which would otherwise be cumbersome.

You can create a notification object with the constructor. However, you don't usually create your own notifications directly. The NSNotificationCenter method postNotification allows you to conveniently post a notification without creating it first.


Notification and Delegation


Using the notification system is similar to using delegates, but it has these advantages:


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.




Method Types


Constructors
NSNotification
Obtaining information about a notification
name
object
userInfo


Constructors



NSNotification

public NSNotification()

Description forthcoming.

public NSNotification( String aName, Object anObject)

Creates a notification object that associates the name aName with the object anObject. aname may not be null.

public NSNotification( String aName, Object anObject, NSDictionary userInfo)

Returns a notification object that associates the name aName with the object anObject and the dictionary of arbitrary data userInfo. The dictionary userInfo may be null. aname may not be null.

See Also: postNotification (NSNotificationCenter)




Instance Methods



name

public String name()

Returns the name of the notification. Examples of this might be "PortIsInvalid" or "PhoneRinging." Typically, you invoke this method on the notification object passed to your notification-handler method. (You specify a notification-handler method when you register to receive the notification.)

Notification names can be any string. To avoid name collisions, however, you might want to use a prefix that's specific to your application.



object

public Object object()

Returns the object associated with the notification. This is often the object that posted this notification. It may be null.

Typically, you invoke this method on the notification object passed in to your notification-handler method. (You specify a notification-handler method when you register to receive the notification.)



userInfo

public NSDictionary userInfo()

Returns the NSDictionary associated with the notification or null if there is no such object. The NSDictionary stores any additional objects that objects receiving the notification might use. For example in the Application Kit, NSControl objects post the ControlTextDidChangeNotification whenever the field editor (an NSText object) changes text inside the NSControl. This notification provides both the NSControl object and the field editor to objects registered to receive it. The field editor is returned when you access the dictionary.


Table of Contents