Skip to content

dannys42/NotificationUserInfoModel

Repository files navigation

NotificationUserInfoModel

CI Status Version License Platform

Description

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.

Example

By declaring your NotificaitonUserInfoModel and creating the approprite serializer and deserializer methods, you can easily interface with NotificationCenter:

Declare Model

struct MyNoteModel {
    // add data fields here
    public var exampleString: String
}

Observer

@objc public func notificationObserver(_ note: Notification) {
    guard let myModel = MyNoteModel(note) else { return }

    /* use myModel  */
}

Emitter

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.

Example Project

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Tested on Xcode 9.2, Swift 4, iOS 10.

Installation

NotificationUserInfoModel is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'NotificationUserInfoModel'

Author

Danny Sung danny@dannysung.com

License

NotificationUserInfoModel is available under the MIT license. See the LICENSE file for more info.

About

A simple protocol to interface with NotificationCenter

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published