Swiftpack.co - Cyberbeni/TypedNotificationCenter as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Cyberbeni/TypedNotificationCenter
Typed version of Apple's NotificationCenter to avoid forgetting setting parameters in the userInfo dictionary and needing to handle not having those parameters.
.package(url: "https://github.com/Cyberbeni/TypedNotificationCenter.git", from: "2.0.0")

TypedNotificationCenter

Build Status Code coverage codebeat badge GitHub release MIT Licence

Platforms Carthage Compatible Swift Package Manager Compatible CocoaPods Compatible

This framework is about rethinking Apple's NotificationCenter to be more typesafe and about removing uncertainity of the needed value being present in the userInfo dictionary. It can be also used to handle NSNotification.Name subscriptions with better performance than NSNotificationCenter.

It is assumed that you already have (or at least know how to make) a DisposeBag-like system for keeping observations alive. (Observations are cleaned up when the observer token is deallocated)

Example usage

Typesafe API

// SampleNotification.swift
import TypedNotificationCenter

enum SampleNotification: TypedNotification {
    struct Payload {
        let type = "test"
    }
    typealias Sender = MyCustomView
}
// OtherFile.swift
// Observe a notification and execute a block with the sender and the payload
var observations = [TypedNotificationObservation](https://raw.github.com/Cyberbeni/TypedNotificationCenter/master/)
observations.append(TypedNotificationCenter.default.observe(SampleNotification.self, object: nil, block: { (sender, payload) in
    print(sender, payload)
}))

// Post a notification
TypedNotificationCenter.default.post(SampleNotification.self, sender: self, payload: SampleNotification.Payload())

// Stop observing the notification, this is also called when the observation object deinitializes
observation?.invalidate()

Typesafe bridging

enum KeyboardWillShowNotification: BridgedNotification {
    public static var notificationName: Notification.Name = UIResponder.keyboardWillShowNotification
    public typealias Sender = NSNull
    public typealias Payload = KeyboardNotificationPayload
}

1:1 drop-in replacement for existing NSNotificationCenter usage

var observations = [TypedNotificationObservation](https://raw.github.com/Cyberbeni/TypedNotificationCenter/master/)
observations.append(TypedNotificationCenter.default.observe(NSLocale.currentLocaleDidChangeNotification, object: nil, queue: NSOperationQueue.mainQueue()) { notification in
    print("The user's locale changed to: \(NSLocale.currentLocale().localeIdentifier)")
})

GitHub

link
Stars: 2
Last commit: 6 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

2.0.0
26 weeks ago

Changes:

  • Using NSLock instead of DispatchQueue to make observation dictionary access thread-safe
  • Added the option to observe NSNotification.Name with improved performance compared to NSNotificationCenter (1 observation will be registered to NSNotificationCenter per NSNotification.Name which then will be forwarded to TypedNotificationCenter)
  • Added input to choose which NSNotificationCenter to use for bridging
  • Added script to create XCFramework

XCFramework built with:

  • Xcode Version 12.4 (12D4e)

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics