Skip to content

Instantly share code, notes, and snippets.

Created January 26, 2015 14:41
  • Star 25 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Typed Notifications
import Foundation
class Box<T> {
let unbox: T
init(_ value: T) { self.unbox = value }
struct Notification<A> {
let name: String
func postNotification<A>(note: Notification<A>, value: A) {
let userInfo = ["value": Box(value)]
NSNotificationCenter.defaultCenter().postNotificationName(, object: nil, userInfo: userInfo)
class NotificationObserver {
let observer: NSObjectProtocol
init<A>(notification: Notification<A>, block aBlock: A -> ()) {
observer = NSNotificationCenter.defaultCenter().addObserverForName(, object: nil, queue: nil) { note in
if let value = (note.userInfo?["value"] as? Box<A>)?.unbox {
} else {
assert(false, "Couldn't understand user info")
deinit {
Copy link

Good one! Did you write code like this for KVO?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment