Giter Site home page Giter Site logo

assassinym / swiftnotificationcenter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 100mango/swiftnotificationcenter

0.0 0.0 1.0 1.14 MB

A Protocol-Oriented NotificationCenter which is type safe, thread safe and with memory safety

License: MIT License

Ruby 5.11% C 0.82% Objective-C 4.60% Swift 89.47%

swiftnotificationcenter's Introduction


A Protocol-Oriented NotificationCenter which is type safe, thread safe and with memory safety.

  • Type Safe

    No more userInfo dictionary and Downcasting, just deliver the concrete type value to the observer.

  • Thread Safe

    You can register, notify, unregister in any thread without crash and data corruption.

  • Memory Safety

    SwiftNotificationCenter store the observer as a zeroing-weak reference. No crash and no need to unregister manually.

It's simple, safe, lightweight and easy to use for one-to-many communication.

Usage

Define protocol and observer:

protocol Update {
    func updateTitle(title: String)
}

extension ViewController: Update {
  func updateTitle(title: String) {
  		self.titleLabel.text = title
  }
}
let vc = ViewController()

Register:

Broadcaster.register(Update.self, observer: vc)

Broadcast:

Broadcaster.notify(Update.self) {
    $0.updateTitle("new title")
}

Unregister:

Broadcaster.unregister(Update.self, observer: self)

Compare with NSNotificationCenter :

For example, handle UIKeyboardWillShowNotification

@objc func handleKeyboardNotification(notification: NSNotification) {
    guard notification.name == NSNotification.Name.UIKeyboardWillShow
        else { return }
    
    guard let beginFrame = (notification
        .userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue
        else { return }
    
    guard let endFrame = (notification
        .userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
        else { return }
    // use beginFrame, endFrame
}

SwiftNotificationCenter way:

/*
If you want to observe the system built in notifications like this.
You can declare a protocol and the relevant method, and use a singleton as a mediator to observe system's notification, then notify our observers.
Please check the refactor example in SwiftNotificationCenterExample Project.
*/
func UIKeyboardWillShow(beginFrame: CGRect, endFrame: CGRect) {
}

Installation

CocoaPods:

pod 'SwiftNotificationCenter'

Carthage:

github "100mango/SwiftNotificationCenter"

Manually:

Just copy source files in the SwiftNotificationCenter folder into your project.

License

SwiftNotificationCenter is under the MIT license.

swiftnotificationcenter's People

Contributors

100mango avatar aceontech avatar edwardmp avatar zsszatmari avatar

Forkers

assassinmt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.