Skip to content

Commit

Permalink
update playgrounds and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Mazyod committed Mar 25, 2017
1 parent a549fdf commit b18debd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
37 changes: 24 additions & 13 deletions Playground/Playground.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,47 @@ import Notificationz
//: # Notificationz
//: ### Helping you _own_ `NSNotificationCenter`!

extension Notification.Name {
static let πŸ’ƒ = Notification.Name("πŸ’ƒ")
static let tenhut = Notification.Name("Ten-hut!")
static let test = Notification.Name("test")
}


//: ## Features
//: Use your own naming convention to wrap NSNotificationCenter

let nsCenter = NSNotificationCenter.defaultCenter()

let nsCenter = NotificationCenter.default
let NC = NotificationCenterAdapter(notificationCenter: nsCenter)
let πŸ“‘ = NotificationCenterAdapter(notificationCenter: nsCenter)
πŸ“‘.post("πŸ’ƒ")
πŸ“‘.post(.πŸ’ƒ)
// Now, you can use NC throughout your app


//: Four simple keywords to remember

class Object {
@objc(call) func call() {}
}

let obj = Object()
NC.add(obj, selector: "call") // normal add observer
NC.observe { notification in } // observe using blocks
NC.post("Ten-hut!") // post a notification
NC.remove(obj) // remove from nsCenter
NC.add(obj, selector: #selector(Object.call)) // normal add observer
NC.observe { notification in } // observe using blocks
NC.post(.tenhut) // post a notification
NC.remove(obj) // remove from nsCenter


//: Transparent and convenient API

let keys = ["observe", "many", "keys"]
let keys = ["observe", "many", "keys"].map { Notification.Name($0) }
NC.observe(keys) { _ in } // observe on the same thread
NC.observeUI(keys) { _ in } // delivered to the main thread

NC.post("string")
NC.post("more-stuff", userInfo: ["info":5])
NC.post(NSNotification(name: "notification", object: nil))
NC.post(.test)
NC.post(.test, userInfo: ["info":5])
NC.post(Notification(name: .test, object: nil))


//: RAII-based observers

Expand All @@ -57,8 +68,8 @@ class Dummy {
}

var dummy: Dummy? = Dummy()
NC.post("call doSomething")
dummy = nil
NC.post("Doesn't crash!")
NC.post(.test) // calls doSomething
dummy = nil // clean up is automatic
NC.post(.test) // doesn't crash!


6 changes: 0 additions & 6 deletions Playground/Playground.playground/timeline.xctimeline

This file was deleted.

32 changes: 13 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ __Use your own naming convention to wrap NotificationCenter__
```swift
let nsCenter = NotificationCenter.default
let πŸ“‘ = NotificationCenterAdapter(notificationCenter: nsCenter)
πŸ“‘.post("πŸ’ƒ")
πŸ“‘.post(.πŸ’ƒ)

// my personal preference, define this in Globals.swift
let NC = NotificationCenterAdapter(notificationCenter: nsCenter)
Expand All @@ -48,26 +48,23 @@ let NC = NotificationCenterAdapter(notificationCenter: nsCenter)
__Four simple keywords to remember__

```swift
NC.add(obj, selector: Selector("call:")) // normal add observer
NC.observe { notification in } // observe using blocks
// it's recommended you define your notifications as enums
let name = Notification.Name(rawValue: "Ten-hut!")
NC.post(name) // post a notification
NC.remove(obj) // remove from nsCenter
let obj = Object()
NC.add(obj, selector: #selector(Object.call)) // normal add observer
NC.observe { notification in } // observe using blocks
NC.post(.tenhut) // post a notification
NC.remove(obj) // remove from nsCenter
```

__Transparent and convenient API__

```swift
let keys = ["observe", "many", "keys"].map {
Notification.Name(rawValue: $0)
}
let keys = ["observe", "many", "keys"].map { Notification.Name($0) }
NC.observe(keys) { _ in } // observe on the same thread
NC.observeUI(keys) { _ in } // delivered to the main thread

NC.post(notificationName)
NC.post(anotherName, userInfo: ["info":5])
NC.post(Notification(name: differentName, object: nil))
NC.post(.test)
NC.post(.test, userInfo: ["info":5])
NC.post(Notification(name: .test, object: nil))
```

__RAII-based observers__
Expand All @@ -92,12 +89,9 @@ class Dummy {
}

var dummy: Dummy? = Dummy()
// trigger notification
NC.post(Notification.Name(rawValue: "call doSomething"))
// cleanup is automatic
dummy = nil
// this won't trigger anything
NC.post("Doesn't crash!")
NC.post(.test) // calls doSomething
dummy = nil // clean up is automatic
NC.post(.test) // doesn't crash!
```

## Getting Started
Expand Down

0 comments on commit b18debd

Please sign in to comment.