Created
November 6, 2014 13:08
-
-
Save njdehoog/d0cc028b48d0a43d173c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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