Skip to content

Instantly share code, notes, and snippets.

@njdehoog
Created November 6, 2014 13:08
Show Gist options
  • Save njdehoog/d0cc028b48d0a43d173c to your computer and use it in GitHub Desktop.
Save njdehoog/d0cc028b48d0a43d173c to your computer and use it in GitHub Desktop.
class Foo: NSObject {
private let bar = Bar()
private var kvoContext = 0
override init() {
super.init()
bar.addObserver(self, forKeyPath: "string", options: .New, context: &kvoContext)
bar.string = "world"
}
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if context == &kvoContext {
println("bar changed: \(bar.string)")
}
else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
deinit {
bar.removeObserver(self, forKeyPath: "string", context: &kvoContext)
}
}
class Bar: NSObject {
dynamic var string = "hello"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment