Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ChaosCoder/Shouter

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Library for One-to-Many communication in Swift Apps

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  
Morty Proxy This is a proxified and sanitized view of the page, visit original site.