class ViewController: UIViewController {
var object = Object()
override func viewDidLoad() {
super.viewDidLoad()
object.addObserver(self, forKeyPath: "propertyOne", options: NSKeyValueObservingOptions.New, context: nil)
object.addObserver(self, forKeyPath: "propertyTwo", options: NSKeyValueObservingOptions.New, context: nil)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "propertyOne" {
print("propertyOne:\(change![NSKeyValueChangeNewKey] as! String)")
} else if keyPath == "propertyTwo" {
print("propertyTwo:\(change![NSKeyValueChangeNewKey] as! Int)")
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
deinit {
self.removeObserver(self, forKeyPath: "propertyOne")
self.removeObserver(self, forKeyPath: "propertyTwo")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
object.propertyOne = "propertyChange"
object.propertyTwo = 2
}
}