NotificationObserver
public final class NotificationObserver <V, S: AnyObject>
An instance of NotificationObserver
is responsible for observing notifications.
Warning
When an observer is initialized, it will immediately begin listening for its specified notification by registering with the specified notification center. To stop listening, dealloc the observer by setting it tonil
.
-
The closure to be called when an
NSNotification
is received.Declaration
Swift
public typealias NotificationClosure = (notification: NSNotification) -> Void
Parameters
notification
The notification received.
-
The closure to be called when a
Notification
is received.Declaration
Swift
public typealias ValueSenderClosure = (value: V, sender: S?) -> Void
Parameters
value
The data sent with the notification.
sender
The object that sent the notification, or
nil
if the notification is not associated with a specific sender.
-
Constructs a new
NotificationObserver
instance and immediately registers to begin observing the specifiednotification
.Warning
To unregister this observer and end listening for notifications, dealloc the object by setting it to
nil
.Declaration
Swift
public convenience init(_ notification: Notification<V, S>, queue: NSOperationQueue? = nil, center: NSNotificationCenter = .defaultCenter(), handler: ValueSenderClosure)
Parameters
notification
The notification for which to register the observer.
queue
The operation queue to which
handler
should be added. Ifnil
(the default), the block is run synchronously on the posting thread.center
The notification center from which the notification should be dispatched. The default is
NSNotificationCenter.defaultCenter()
.handler
The closure to execute when the notification is received.
Return Value
A new
NotificationObserver
instance. -
Constructs a new
NotificationObserver
instance and immediately registers to begin observing the specifiednotification
.Warning
To unregister this observer and end listening for notifications, dealloc the object by setting it to
nil
.Declaration
Swift
public init(_ notification: Notification<V, S>, queue: NSOperationQueue? = nil, center: NSNotificationCenter = .defaultCenter(), handler: NotificationClosure)
Parameters
notification
The notification for which to register the observer.
queue
The operation queue to which
handler
should be added. Ifnil
(the default), the block is run synchronously on the posting thread.center
The notification center from which the notification should be dispatched. The default is
NSNotificationCenter.defaultCenter()
.handler
The closure to execute when the notification is received.
Return Value
A new
NotificationObserver
instance.