Latest 0.5
Homepage https://github.com/tralf/SimpleNotification
License MIT
Platforms ios 10.0
Authors

CI Status

SimpleNotification is a lightweight wrapper, that helps you to deal with observer pattern easier, than using good old buddy NotificationCenter.

Features

  • Deals with closures, not selectors
  • Uses powerful typed observer blocks with user data object type defined at compilation time
  • Supports both custom and system notifications

Example

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

Simple example of observing a notification, which is expected to send Data object:

// Registering for a notification
let dumbDateObserver: NotificationObserver<Date> = NotificationObserver(event: "Example event") { item in
    // item variable is guaranteed to be type of Date
}

// Sending a notification
NotificationEmitter.fire(event: "Example event", associatedObject: Date())

If it doesn’t matter what type of object is send as user data with a notification or it sends nothing:

// Registering for a notification
let dumbNilObserver: NotificationObserver<Any> = NotificationObserver(event: "Another event") { _ in
    // Your code here
}

// Sending a notification
NotificationEmitter.fire(event: "Another event")

A reasonable question: "What happens if I expect one type of object, but the other is sent?"
The answer: it depends on your needs. When registering for an event, you may pass an optional argument policy. There are 3 options: to raise a fatal error, to raise an assert or just skip. By default it uses .assert value, but feel free to use any.

let dateObserver = NotificationObserver<Date>(event: "Example event", policy: fatalError("Wow, so unexpected!")) { item in
    // Your code here
}

// Causes fatal error
NotificationEmitter.fire(event: "Example event", associatedObject: "I'm a string!")

However, if default behaviour in case of unexpected associated type isn’t that you need, you can handle it yourself by passing a inconsistentObjectHandler parameter.

Installation

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

pod 'SimpleNotification'

Author

Viktor Kalinchuk, [email protected]

License

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

Latest podspec

{
    "name": "SimpleNotification",
    "version": "0.5",
    "summary": "A lightweight wrapper for native notifications with typed event observers.",
    "description": "A lightweight wrapper for native notifications.nFeatures:n1. Easy-to-use Swift-style replacement for Notification and NotificationCenter functionalityn2. Closure-based - no need to deal with selectors anymoren3. Uses the power of Swift generics. Observers wait for a user data with specific type - they get it!",
    "homepage": "https://github.com/tralf/SimpleNotification",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Viktor Kalinchuk": "[email protected]"
    },
    "source": {
        "git": "https://github.com/tralf/SimpleNotification.git",
        "tag": "0.5"
    },
    "platforms": {
        "ios": "10.0"
    },
    "swift_version": "5.0",
    "source_files": "SimpleNotification/Source/**/*.swift"
}

Pin It on Pinterest

Share This