Swiftpack.co - Package - ChaosCoder/Shouter
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

Shouter is a simple, safe, lightweight way for one-to-many communication. It is a type, memory and thread safe alternative for NotificationCenter.

  • Type Safe: No more userInfo: [String: Any] dictionary guarded casting.
  • Thread Safe: You can register, notify, unregister in any thread without crash and data corruption.
  • Memory Safe: Shouter stores observers as a zeroing-weak reference by using NSHashTable. No crash and no need to unregister manually before deallocating.

Usage

Define a protocol, that your observer implements:

protocol SomeNotification {
  func somethingHappened(value: String)
}

class ViewController: UIViewController { /* ... */ }

extension ViewController: SomeNotification {
  func somethingHappened(value: String) {
    self.titleLabel.text = "Something happened: \(value)"
  }
}

Have an observer:

let vc = ViewController()

Register the observer for the notification:

Shouter.default.register(SomeNotification.self, observer: vc)

Notify all observers:

Shouter.default.notify(SomeNotification.self) {
  $0.somethingHappened(value: "Hello World")
}

Unregister, when the observer is not interested in the notification anymore:

Shouter.default.unregister(SomeNotification.self, observer: vc)

Installation

CocoaPods:

pod 'Shouter'

Swift Package Manager:

.package(url: "https://github.com/ChaosCoder/Shouter.git", from: "0.5.0")

Acknowledgments

Shouter was inspired and partially based on the library 100mango/SwiftNotificationCenter.
The logo is based on the "Broadcast" icon by Amy Chiang from the Noun Project.

Github

link
Stars: 2