Class

NSNotification

An object containing information broadcast to registered observers that bridges to Notification; use NSNotification when you need reference semantics or other Foundation-specific behavior.

Overview

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.

You can create a notification object with the class methods init(name:object:) or notificationWithName:object:userInfo:. However, you don’t usually create your own notifications directly. The NSNotificationCenter methods post(name:object:) and post(name:object:userInfo:) allow you to conveniently post a notification without creating it first.

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.

NSNotification 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 [super init]. NSNotification is not meant to be instantiated directly, and its init method raises an exception.

Topics

Creating Notifications

init?(coder: NSCoder)

Initializes a notification with the data from an unarchiver.

init(name: NSNotification.Name, object: Any?)

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

init(name: NSNotification.Name, object: Any?, userInfo: [AnyHashable : Any]? = nil)

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

struct NSNotification.Name

The type used for the name of a notification.

Getting Notification Information

var name: NSNotification.Name

The name of the notification.

var object: Any?

The object associated with the notification.

var userInfo: [AnyHashable : Any]?

The user information dictionary associated with the receiver.

Relationships

Inherits From

See Also

Using Reference Types

typealias Notification.ReferenceType

An alias for this value type's equivalent reference type.