• This repository has been archived on 13/Jul/2019
  • Stars
    star
    167
  • Rank 226,592 (Top 5 %)
  • Language
    Swift
  • License
    Other
  • Created almost 10 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[DEPRECATED] Generic notifications and observers for Cocoa and CocoaTouch

No Maintenance Intended

⚠️ Deprecated ⚠️

As Swift 3.0 and proposal SE-0069, this library is no longer necessary.

Foundation now provides a Notification 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 Status Version Status license MIT codecov Platform Carthage compatible

Generic notifications and observers for Cocoa and CocoaTouch, inspired by objc.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 to NSNotificationCenter. See objc.io's snippet #16 on Typed 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 cocoapods
pod 'JSQNotificationObserverKit'

# Feeling adventurous? Get the latest on develop
pod 'JSQNotificationObserverKit', :git => 'https://github.com/jessesquires/JSQNotificationObserverKit.git', :branch => 'develop'

Carthage

github "jessesquires/JSQNotificationObserverKit"

Documentation

Read the docs. Generated with jazzy. Hosted by GitHub Pages. More information on the gh-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 changes
let myView = UIView()

// This notification posts a CGSize value from a UIView sender
let notification = Notification<CGSize, UIView>(name: "NewViewSizeNotif", sender: myView)

// This observer listens for the notification described above
var observer: NotificationObserver<CGSize, UIView>?

// Register observer, start listening for the notification
observer = NotificationObserver(notification) { (value, sender) in
    // handle notification
    // the value and sender are both passed here
}

// Post the notification with the updated CGSize value
notification.post(CGSizeMake(200, 200))

// 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
notification.post("new string")

// Register observer, this handles notifications from *any* sender
var observer: NotificationObserver<String, AnyObject>?
observer = NotificationObserver(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(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 example UIApplicationDidReceiveMemoryWarningNotification.

let notification = Notification<Any?, AnyObject>(name: "MyEventNotification")

let observer = NotificationObserver(notification) { (value, sender) in
    // handle notification
    // value is nil, sender is nil
}

// notification value is `Any?`, so pass nil
notification.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 full NSNotification object.

let notification = CocoaNotification(name: UIApplicationDidReceiveMemoryWarningNotification)

let observer = 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 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 © 2014-present Jesse Squires.

Please provide attribution, it is greatly appreciated.

More Repositories

1

JSQMessagesViewController

An elegant messages UI library for iOS
Objective-C
11,142
star
2

JSQDataSourcesKit

⚠️ Deprecated ⚠️
Swift
683
star
3

JSQCoreDataKit

A swifter Core Data stack
Swift
615
star
4

JSQSystemSoundPlayer

A fancy Obj-C wrapper for Cocoa System Sound Services
Objective-C
581
star
5

PresenterKit

⚠️ Deprecated ⚠️
Swift
551
star
6

Foil

A lightweight property wrapper for UserDefaults done right
Swift
457
star
7

JSQWebViewController

[Deprecated] A lightweight Swift WebKit view controller for iOS
Swift
305
star
8

TIL

Things I've learned and/or things I want to remember. Notes, links, advice, example code, etc.
282
star
9

Nine41

Automate overriding the status bars for all running iOS simulators
Swift
160
star
10

JSQFlatButton

[DEPRECATED] A light-weight, flat design UIButton for iOS
Objective-C
120
star
11

DefaultStringConvertible

[DEPRECATED] A default CustomStringConvertible implementation for Swift types
Swift
119
star
12

swift-sorts

A collection of sorting algorithms implemented in Swift
Swift
106
star
13

esoteric-swift

A collection of obscure Swift programs. The less comprehensible, the better — just like normal code.
Swift
96
star
14

app-icons-script

Photoshop script to generate all iOS and macOS app icons
JavaScript
84
star
15

ReactiveCollectionsKit

Data-driven, declarative, reactive, diffable collections (and lists!) for iOS. A modern, fast, and flexible library for UICollectionView done right.
Swift
81
star
16

swift-proposal-analyzer

An analysis of Swift Evolution proposals
Swift
58
star
17

objc-sorts

A collection of sorting algorithms implemented in Objective-C
Objective-C
52
star
18

gh-workflows

A collection of useful generic GitHub Actions workflows
50
star
19

safari-tabs-to-omnifocus

AppleScript to export current Safari tabs to OmniFocus inbox items
49
star
20

jessesquires.com

Turing complete with a stack of 0xdeadbeef
HTML
45
star
21

JSQActivityKit

[DEPRECATED] Swift UIActivities for iOS
Swift
43
star
22

delete-uber

"There is no place for ethics in this business sweetheart." — a list of news articles (with excerpts) on Uber.
33
star
23

.github

Default community health files for all of my open-source projects
29
star
24

GrandSugarDispatch

[DEPRECATED] Syntactic sugar for Grand Central Dispatch (GCD)
Swift
27
star
25

ios-watchdog

Main thread watchdog for iOS
Objective-C
26
star
26

talks

Slides and materials for talks that I've given
Rich Text Format
25
star
27

template-iOS-macOS

Template repository for my iOS and macOS projects
Ruby
24
star
28

xcode-settings-backup

Backup of my Xcode configuration and various settings
Shell
21
star
29

icloud-backup-script

Backup your iCloud Drive documents using rsync
Shell
20
star
30

wwdc-notes

Taking notes on WWDC videos, new APIs, and announcements
13
star
31

drumpf

Safari Extensions are dead 💀 RIP.
JavaScript
10
star
32

playdate-learning

Learning how to make games for Playdate https://play.date
8
star
33

template-jekyll-site

Template repository for Jekyll websites
HTML
8
star
34

c-sorts

A collection of sorting algorithms implemented in C
C
8
star
35

FreedomFarts

Join the Fart Party! In God, We Fart.
Objective-C
5
star
36

rdar-19368054

Example project showing Swift compiler bug, rdar://19368054
Swift
2
star
37

likeyoungrecords

Website for Like Young Records. RIP.
PHP
2
star
38

a-world-without-police

A world without police
2
star
39

15puzzle.js

A 15 puzzle written in javascript - just a fun coding exercise
JavaScript
1
star
40

jessesquires.github.io

Open source projects homepage
1
star
41

sandbox-gh-workflows

A sandbox repo for testing GitHub Actions workflows
Ruby
1
star