NotificationUserInfoModel is a simple set of convenience protocols to make passing data via NotificationCenter more Swift-like.
There are plenty of other NotificationCenter wrappers out there. NotificationUserInfoModel is not intended to compete with them. Rather, if you plan on using NotificationCenter API's as-is, then this simply helps reduce some boiler-plate code that may be error-prone.
By declaring your NotificaitonUserInfoModel and creating the approprite serializer and deserializer methods, you can easily interface with NotificationCenter:
struct MyNoteModel {
// add data fields here
public var exampleString: String
}
@objc public func notificationObserver(_ note: Notification) {
guard let myModel = MyNoteModel(note) else { return }
/* use myModel */
}
let model = MyNoteModel(exampleString: "Example")
NotificationCenter.default.post(name: .ExampleNotification, object: nil, userInfo: model)
For a complete a more complete example, please see the included sample project.
To run the example project, clone the repo, and run pod install
from the Example directory first.
Tested on Xcode 9.2, Swift 4, iOS 10.
NotificationUserInfoModel is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'NotificationUserInfoModel'
Danny Sung danny@dannysung.com
NotificationUserInfoModel is available under the MIT license. See the LICENSE file for more info.