SwiftNotifications is a Swift framework providing a strong typed wrapper around the functionality of NotificationCenter.
To install SwiftNotifications using Carthage, add github "nbcnews/SwiftNotifications"
to your Cartfile, then follow these steps.
Before notification can be sent or received, it needs to be defined. Notifications are defined by creating struct (or class) conforming to eitherCodableNotification
, PostableNotification
or ObservableNotification
protocols.
Conforming to CodableNotification
is the simplest way to define notification:
struct MyNotification: CodableNotification {
let intValue: Int
let stringValue: String
let structValue: MyStruct
let classValue: MyClass
}
Notification can be empty if no additional information needs to be provided:
struct MyEmptyNotification: CodableNotification {
}
One limitation of CodableNotification
is that all stored properties must support the Codable
protocol. In some situations
that is not possible. In this case, you can implement PostableNotification
protocol for notifications that can be posted, or
ObservableNotification
for notification that can be observed but not posted.
ObservableNotification
must implement init?(notification:)
struct MyNotification: ObservableNotification {
let value: NonCodableType
init?(notification: Notification) {
guard userInfo = notification.userInfo else {
return nil
}
guard let value = userInfo["valueKey"] as? NonCodableType else {
return nil
}
self.value = value
}
}
PostableNotification
in addition to init
sould also implement var userInfo: [AnyHashable: Any]?
:
struct MyNotification: PostableNotification {
let value: NonCodableType
var userInfo: [AnyHashable: Any]? {
return ["valueKey": value]
}
init?(notification: Notification) {
guard userInfo = notification.userInfo else {
return nil
}
guard let value = userInfo["valueKey"] as? NonCodableType else {
return nil
}
self.value = value
}
}
By default, notification name is the name of the struct combined with the module name. For example, notification struct MyNotification
located in a module named MyModule
will have the name MyModule.MyNotification
. But sometimes it is
necessary to define the notification name to be different from struct's name. It can be done by defining the static property
name
in you notification
struct MyNotification: ObservableNotification {
static let name = "MySpecialNotificationName"
}
Sending a notification is as simple as calling post()
method on a notification object
Mynotification().post()
You can send notification to custom NotificationCenter
Mynotification().post(notificationCenter)
Send notification with information about sender
Mynotification().post(from: object)
class Listener {
let observer = NotificationObserver<MyNotification>()
init() {
observer.observe(self, Listener.handleNotification)
}
func handleNotification(notification: MyNotification) {
...
}
}
In most cases, no special action is needed to stop receving notifications because notifications will stop automaticaly when NotificationObserver
is released. In cases that more precise control is required, notifications can be stoped by calling remove()
method of NotificationObserver
observer.remove()