Cyberbeni/TypedNotificationCenter
Typed version of Apple's NotificationCenter to avoid forgetting setting parameters in the userInfo dictionary and needing to handle not having those parameters.
Example usage
Typesafe API
// SampleNotification.swift
import TypedNotificationCenter
enum SampleNotification: TypedNotification {
struct Payload {
let type = "test"
}
typealias Sender = MyCustomView
}// OtherFile.swift
// Observe a notification and execute a block with the sender and the payload
var observations = [TypedNotificationObservation]()
observations.append(TypedNotificationCenter.default.observe(SampleNotification.self, object: nil, block: { (sender, payload) in
print(sender, payload)
}))
// Post a notification
TypedNotificationCenter.default.post(SampleNotification.self, sender: self, payload: SampleNotification.Payload())
// Stop observing the notification, this is also called when the observation object deinitializes
observation?.invalidate()Typesafe bridging
enum KeyboardWillShowNotification: BridgedNotification {
public static var notificationName: Notification.Name = UIResponder.keyboardWillShowNotification
public typealias Sender = NSNull
public typealias Payload = KeyboardNotificationPayload
}1:1 drop-in replacement for existing NSNotificationCenter usage
var observations = [TypedNotificationObservation]()
observations.append(TypedNotificationCenter.default.observe(NSLocale.currentLocaleDidChangeNotification, object: nil, queue: NSOperationQueue.mainQueue()) { notification in
print("The user's locale changed to: \(NSLocale.currentLocale().localeIdentifier)")
})Package Metadata
Repository: Cyberbeni/TypedNotificationCenter
Stars: 7
Forks: 0
Open issues: 1
Default branch: master
Primary language: swift
License: MIT
Topics: carthage, ios, linux, macos, swift
README: README.md