v-silin / SimplifiedNotificationCenter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SimplifiedNotificationCenter

CI Status Version License Platform Readme Score

This is a tiny swift wrapper around NSNotificationCenter with generics, that aids in the creation of Notifications.

Installation

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

pod "SimplifiedNotificationCenter"

If you using swift 2.2, add the following line to your Podfile:

pod "SimplifiedNotificationCenter",
:git => 'https://github.com/0x384c0/SimplifiedNotificationCenter.git',
:branch => 'swift2.2'

Usage

Add import SimplifiedNotificationCenter to your source code

// create notification
let notification = SimpleNotification<String>(name: "Example.notification")

// subscribe
notification.subscribe {(value, sender) in
    print("sender: \(sender.dynamicType)")
    print("value:  \(value.title)")
}

// post
notification.post("sample text")
//sample text and sender will be printed

Passing notifications between different places of application

Notifications holder:

class Notifications{
    let testNotification    = SimpleNotification<String>         (name: "Example.testNotification")
}

Notifications handler:

class SampleClass {
        //instance of notifications holder
        var notifications = Notifications()
        init(){
            //subscribe
            notifications.testNotification.subscribe{(value, _) in
                print("value: \(value)")
        }
    }
}

Notifications caller:

class AnotherClass {
    func post(){
        //post
        Notifications().testNotification.post("comunicationBetweenDifferentClassesExample Test text")
        //or use
        //SimpleNotification<String>(name: "Example.testNotification").post("comunicationBetweenDifferentClassesExample Test text")
        //but it is not safe
    }
}

Example:

let
sampleClass = SampleClass(),
anotherClass = AnotherClass()
anotherClass.post()
//comunicationBetweenDifferentClassesExample Test text will be printed

Example App

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

Requirements

  • ios 8.0 and higher
  • swift v3.0

Unit Tests

SimplifiedNotificationCenter includes a suite of unit tests within the Tests subdirectory. These tests can be run simply be executed the test action on the platform framework you would like to test.

Author

0x384c0, 0x384c0@gmail.com

License

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

About

License:MIT License


Languages

Language:Shell 57.6%Language:Swift 35.2%Language:Objective-C 3.6%Language:Ruby 3.5%