Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9
[DEPRECATED] Generic notifications and observers for Cocoa and CocoaTouch
License
jessesquires/JSQNotificationObserverKit
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
As Swift 3.0 and proposalSE-0069, this library is no longer necessary.
Foundation now provides aNotification
struct. Unfortunately, it is not generic like the value type provided by this library,Notification<T, U>
, but dealing with the trouble/confusion between the namespaces (Foundation.Notification
vs.JSQNotificationObserverKit.Notification
) makes this library not worth it.
Generic notifications and observers for Cocoa and CocoaTouch, inspired byobjc.io
This library 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-safety and a cleaner interface toNSNotificationCenter
. See objc.io'ssnippet #16 onTyped Notification Observers for more information.
- Xcode 7.3+
- iOS 8.0+
- OSX 10.10+
- tvOS 9.1+
- watchOS 2.0+
- Swift 2.2
CocoaPods (recommended)
use_frameworks!# For latest release in cocoapodspod'JSQNotificationObserverKit'# Feeling adventurous? Get the latest on developpod'JSQNotificationObserverKit',:git=>'https://github.com/jessesquires/JSQNotificationObserverKit.git',:branch=>'develop'
github"jessesquires/JSQNotificationObserverKit"
Read thedocs. Generated withjazzy. Hosted byGitHub Pages. More information on thegh-pages
branch.
import JSQNotificationObserverKit
See the included unit tests for more examples and usage.
// Suppose we have a UIView that posts a notification when its size changesletmyView=UIView()// This notification posts a CGSize value from a UIView senderletnotification=Notification<CGSize,UIView>(name:"NewViewSizeNotif", sender: myView)// This observer listens for the notification described abovevarobserver:NotificationObserver<CGSize,UIView>?// Register observer, start listening for the notificationobserver=NotificationObserver(notification){(value, sender)in // handle notification // the value and sender are both passed here}// Post the notification with the updated CGSize valuenotification.post(CGSizeMake(200,200))// Unregister observer, stop listening for notificationsobserver=nil
Not all notifications are associated with a specific sender object. Here's how to handlenil
sender inJSQNotificationObserverKit
. This observer will respond to notificationsregardless of the instances sending them.
// This notification posts a string value, the sender is nilletnotification=Notification<String,AnyObject>(name:"StringNotif")// Post the notificationnotification.post("new string")// Register observer, this handles notifications from *any* sendervarobserver:NotificationObserver<String,AnyObject>?observer=NotificationObserver(notification){(value, sender)in // handle notification // the value is passed here, sender is nil}// unregister observer, stop listening for notificationsobserver=nil
You can optionally pass anNSOperationQueue
andNSNotificationCenter
. The default values arenil
andNSNotificationCenter.defaultCenter()
, respectively.
// Initialize an observer and post a notification// with a custom notification center and operation queueletc=NSNotificationCenter.defaultCenter()letq=NSOperationQueue.mainQueue()letobserver=NotificationObserver(n, queue: q, center: c){(value, sender)in // handle notification}notification.post(v, center: c)
Not all notifications are associated with a specific value. Sometimes notifications are used to simply broadcast an event, for exampleUIApplicationDidReceiveMemoryWarningNotification
.
letnotification=Notification<Any?,AnyObject>(name:"MyEventNotification")letobserver=NotificationObserver(notification){(value, sender)in // handle notification // value is nil, sender is nil}// notification value is `Any?`, so pass nilnotification.post(nil)
The library can also handle "traditional" notifications that are posted by the OS. Instead of using the(value, sender)
handler, use the(notification)
handler which passes the fullNSNotification
object.
letnotification=CocoaNotification(name: UIApplicationDidReceiveMemoryWarningNotification)letobserver=CocoaObserver(notification, handler:{(notification:NSNotification)in // handle the notification})// the notification will be posted by iOS
There's a suite of unit tests for theJSQNotificationObserverKit.framework
. To run them, openJSQNotificationObserverKit.xcodeproj
, select theJSQNotificationObserverKit
scheme, then ⌘-u.
These tests are well commented and serve as further documentation for how to use this library.
Please follow these sweetcontribution guidelines.
Created and maintained by@jesse_squires.
JSQNotificationObserverKit
is released under anMIT License. SeeLICENSE
for details.
Copyright © 2014-present Jesse Squires.
Please provide attribution, it is greatly appreciated.
About
[DEPRECATED] Generic notifications and observers for Cocoa and CocoaTouch
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.