Generic notifications and observers for iOS, inspired by objc.io
This framework aims to provide better semantics regarding notifications and moves the responsibilty of observing and handling notifications to a lightweight, single-purpose object. It also brings type-saftey and a cleaner interface to NSNotificationCenter
. See objc.io's snippet #16 on Typed Notification Observers for more information.
- iOS 8
- Swift 1.1
From CocoaPods:
use_frameworks!
# For latest release in cocoapods
pod 'JSQNotificationObserverKit'
# Feeling adventurous? Get the latest on develop
pod 'JSQNotificationObserverKit', :git => 'https://github.com/jessesquires/JSQNotificationObserverKit.git', :branch => 'develop'
Manually:
Add the NotificationObserver.swift
file to your project.
Read the fucking docs. Generated with jazzy. Hosted by GitHub Pages.
# regenerate documentation
$ cd /path/to/JSQNotificationObserverKit/
$ ./build_docs.sh
$ open _docs/
import JSQNotificationObserverKit
See the included unit tests for more examples and usage.
// Suppose we have a UIView that posts a notification when its size changes
let myView = UIView()
// This notification posts a CGSize value from a UIView sender
let notification = Notification<CGSize, UIView>(name: "NewViewSizeNotif", sender: myView)
// Register observer, start lisening for the notification
var observer: NotificationObserver<CGSize, UIView>?
observer = NotificationObserver(notification: notification) { (value, sender) in
// handle notification
// the value and sender are both passed here
}
// Post the notification with the updated CGSize value
let newViewSize = CGSizeMake(200, 200)
postNotification(notification, value: newViewSize)
// unregister observer, stop listening for notifications
observer = nil
Not all notifications are associated with a specific sender object. Here's how to handle nil
sender in JSQNotificationObserverKit
. This observer will respond to notifications regardless of the instances sending them.
// This notification posts a string value, the sender is nil
let notification = Notification<String, AnyObject>(name: "StringNotif")
// Post the notification
postNotification(notification, value: "new string")
// Register observer, this handles notifications from *any* sender
var observer: NotificationObserver<String, AnyObject>?
observer = NotificationObserver(notification: notification) { (value, sender) in
// handle notification
// the value is passed here, sender is nil
}
// unregister observer, stop listening for notifications
observer = nil
You can optionally pass an NSOperationQueue
and NSNotificationCenter
. The default values are nil
and NSNotificationCenter.defaultCenter()
, respectively.
// Initialize an observer and post a notification
// with a custom notification center and operation queue
let c = NSNotificationCenter.defaultCenter()
let q = NSOperationQueue.mainQueue()
let observer = NotificationObserver(notification: n, queue: q, center: c) { (value, sender) in
// handle notification
}
postNotification(n, value: v, center: c)
Not all notifications are associated with a specific value, for example UIApplicationDidReceiveMemoryWarningNotification
.
let notification = Notification<Void, AnyObject>(name: UIApplicationDidReceiveMemoryWarningNotification)
let observer = NotificationObserver(notification: notification) { (value, sender) -> Void in
// handle notification
// value is an empty tuple, sender is nil
}
// notification value is `Void`, so pass the empty tuple `()`
postNotification(notification, value: ())
There's a suite of unit tests for the JSQNotificationObserverKit.framework
. To run them, open JSQNotificationObserverKit.xcodeproj
, select the JSQNotificationObserverKit
scheme, then โ-u.
These tests are well commented and serve as further documentation for how to use this library.
Please follow these sweet contribution guidelines.
Created and maintained by @jesse_squires
JSQNotificationObserverKit
is released under an MIT License. See LICENSE
for details.
Copyright ยฉ 2015 Jesse Squires.
Please provide attribution, it is greatly appreciated.