Key Value Observing (KVO)
private var globalContext = 0
class Participant: NSObject {
dynamic var name = "Maria"
}
class Observer: NSObject {
var participant = Participant()
override init() {
super.init()
participant.addObserver(self, forKeyPath: "name", options: .New, context: &globalContext)
}
override func observeValueForKeyPath(keyPath: String?,
ofObject object: AnyObject?,
change: [String : AnyObject]?,
context: UnsafeMutablePointer) {
if context == &globalContext {
if let newValue = change?[NSKeyValueChangeNewKey] {
print("Date changed: \(newValue)")
}
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
deinit {
participant.removeObserver(self, forKeyPath: "name", context: &globalContext)
}
}