TypedNotification

public protocol TypedNotification

A notification that can be posted by a TypedNotificationCenter instance.

TypedNotification provides a type-safe alternative to Foundation’s Notification type. All conforming types must specify the type of the object attached to the notification. Conforming types can attach additional data to the notification as properties. This replaces stringly typed userInfo dictionary attached to Foundation Notifications

Conforming to TypedNotification

Conforming types must declare the type of the object attached to the notification and provide storage for it. For example, a notification posted by a DataStore object might look like this:

struct DataStoreDidSaveNotification: TypedNotification {
    /// The data store posting the notification.
    let object: DataStore
}

Customising the Notification Name

A default notification name for conforming types is generated in a protocol extension. The name consists of the name of the notification type prefixed by AJJ. You can specify a different name by implementing the static name property on your notification types:

struct DataStoreDidSaveNotification: TypedNotification {

    static let name = Notification.Name("XYZDataStoreDidSave")

    /// The data store posting the notification.
    let object: DataStore
}
  • The type of an object attached to the notification.

    Declaration

    Swift

    associatedtype Object
  • name Default implementation

    The name of the notification that identifies it.

    The default implementation returns the name of Self prefixed with AJJ.

    Default Implementation

    Declaration

    Swift

    static var name: Notification.Name { get }
  • An object attached to the notification.

    Declaration

    Swift

    var object: Object { get }