Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings
This repository was archived by the owner on Jul 12, 2019. It is now read-only.

[DEPRECATED] Generic notifications and observers for Cocoa and CocoaTouch

License

NotificationsYou must be signed in to change notification settings

jessesquires/JSQNotificationObserverKit

Repository files navigation

No Maintenance Intended

⚠️ Deprecated⚠️

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.

JSQNotificationObserverKit

Build StatusVersion Statuslicense MITcodecovPlatformCarthage compatible

Generic notifications and observers for Cocoa and CocoaTouch, inspired byobjc.io

About

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.

Requirements

  • Xcode 7.3+
  • iOS 8.0+
  • OSX 10.10+
  • tvOS 9.1+
  • watchOS 2.0+
  • Swift 2.2

Installation

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"

Documentation

Read thedocs. Generated withjazzy. Hosted byGitHub Pages. More information on thegh-pages branch.

Getting Started

import JSQNotificationObserverKit

See the included unit tests for more examples and usage.

Example

// 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

Notifications without a sender

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

Using a custom queue and notification center

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)

Notifications without a value

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)

Working with "traditional" Cocoa notifications

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

Unit tests

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.

Contribute

Please follow these sweetcontribution guidelines.

Credits

Created and maintained by@jesse_squires.

License

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

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp