Giter VIP home page Giter VIP logo

jsqnotificationobserverkit's Introduction

JSQNotificationObserverKit

Build Status Version Status license MIT

Generic notifications and observers for iOS, inspired by objc.io

About

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.

Requirements

  • iOS 8
  • Swift 1.1

Installation

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.

Documentation

Read the fucking docs. Generated with jazzy. Hosted by GitHub Pages.

# regenerate documentation
$ cd /path/to/JSQNotificationObserverKit/
$ ./build_docs.sh
$ open _docs/

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

Notifications without a sender

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

Using a custom queue and notification center

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)

Notifications without a value

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: ())

Unit tests

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.

Contribute

Please follow these sweet contribution guidelines.

Credits

Created and maintained by @jesse_squires

License

JSQNotificationObserverKit is released under an MIT License. See LICENSE for details.

Copyright ยฉ 2015 Jesse Squires.

Please provide attribution, it is greatly appreciated.

jsqnotificationobserverkit's People

Contributors

jessesquires avatar

Watchers

Carabineiro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.