iOS Developer Library — Pre-Release

Developer

Foundation Framework Reference NSNotification Class Reference

Options
Deployment Target:

On This Page
Language:

NSNotification

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. More...

Inheritance


Conforms To


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in iOS 2.0 and later.
  • Returns a new notification object with a specified name and object.

    Declaration

    Swift

    convenience init(name aName: String, object anObject: AnyObject?)

    Objective-C

    + (instancetype)notificationWithName:(NSString *)aName object:(id)anObject

    Parameters

    aName

    The name for the new notification. May not be nil.

    anObject

    The object for the new notification.

    Import Statement

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

    See Also

    postNotificationName:object: (NSNotificationCenter)

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

    Declaration

    Objective-C

    + (instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)userInfo

    Parameters

    aName

    The name for the new notification. May not be nil.

    anObject

    The object for the new notification.

    userInfo

    The user information dictionary for the new notification. May be nil.

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    init(name aName: String, object object: AnyObject?, userInfo userInfo: [NSObject : AnyObject]?)

    Objective-C

    - (instancetype)initWithName:(NSString *)aName object:(id)object userInfo:(NSDictionary *)userInfo

    Parameters

    aName

    The name for the new notification. May not be nil.

    object

    The object for the new notification.

    userInfo

    The user information dictionary for the new notification. May be nil.

    Import Statement

    Swift

    import Foundation

    Availability

    Available in iOS 4.0 and later.

  • name name Property

    The name of the notification. (read-only)

    Declaration

    Swift

    var name: String { get }

    Objective-C

    @property(readonly, copy) NSString *name

    Discussion

    Typically you use this property to find out what kind of notification you are dealing with when you receive a notification.

    Special Considerations

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

    Import Statement

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • object object Property

    The object associated with the notification. (read-only)

    Declaration

    Swift

    var object: AnyObject? { get }

    Objective-C

    @property(readonly, retain) id object

    Discussion

    This is often the object that posted this notification. It may be nil.

    Typically you use this method to find out what object a notification applies to when you receive a notification.

    For example, suppose you’ve registered an object to receive the message handlePortDeath: when the “PortInvalid” notification is posted to the notification center and that handlePortDeath: needs to access the object monitoring the port that is now invalid. handlePortDeath: can retrieve that object as shown here:

    • - (void)handlePortDeath:(NSNotification *)notification
    • {
    • ...
    • [self reclaimResourcesForPort:notification.object];
    • ...
    • }

    Import Statement

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • userInfo userInfo Property

    The user information dictionary associated with the receiver. (read-only)

    Declaration

    Swift

    var userInfo: [NSObject : AnyObject]? { get }

    Objective-C

    @property(readonly, copy) NSDictionary *userInfo

    Discussion

    May be nil.

    The user information dictionary stores any additional objects that objects receiving the notification might use.

    For example, in the Application Kit, NSControl objects post the NSControlTextDidChangeNotification whenever the field editor (an NSText object) changes text inside the NSControl. This notification provides the NSControl object as the notification's associated object. In order to provide access to the field editor, the NSControl object posting the notification adds the field editor to the notification's user information dictionary. Objects receiving the notification can access the field editor and the NSControl object posting the notification as follows:

    • - (void)controlTextDidBeginEditing:(NSNotification *)notification
    • {
    • NSText *fieldEditor = [notification.userInfo
    • objectForKey:@"NSFieldEditor"]; // the field editor
    • NSControl *postingObject = notification.object; // the object that posted the notification
    • ...
    • }

    Import Statement

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.