Key value observing is a feature from Objective C. It depends on dynamically dispatched methods. You might ask what thats means, well dynamically dispatched methods are those instance methods of an object which are run by querying the related object. Objective C supports dynamic dispatching. But dynamic dispatching comes with a disadvantage of its low performance. Because in dynamic dispatching first the object is asked which code is to execute for this particular method, this query is asked every time the method is called. But Swift supports static dispatching means when you call a particular method of an object CPU jumps to it directly, without asking any query to corresponding object. This is the main reason that Swift is much faster than Objective C. And as Key Value Observing feature only works with Dynamically Dispatched method, will not work with statically dispatching.
For example, let the properties that uses dynamic dispatching are:
"dynamic var variable1: Float = 0.0"
"dynamic var variable2: Float = 0.0"
The function that that will observe the change in the properties is:
"addObserver(_:,forKeyPath:, options:, context:)"
For above properties this function looks like:
Here in this function "self" is used for the Controller which is the observer of whatever change you make in these properties. Now every time whenever dynamic marked properties changes you also should override a method that will execute a block of code correspondingly, here is that method:
"override func observeValueForKeyPath(ketPath_:,ofObject:,change:,context:)"
According to my properties here is the method that will do:
But here Swift, because, supports both Statically dispatching as well as Dynamically dispatching, is tops Objective C. All you need to do to use Key Value Observing in Swift is just to use dynamic dispatching on properties && methods that you want to follow this feature. Also the class whose properties you want to follow KVO should inherits NSObject superclass. By doing so you will make this class an Objective C class && you are able to use Dynamically Dispatching. Now the properties of this class (NSObject subclass) that you want to follow Dynamic Dispatching are marked with "dynamic" keyword. You can mark "dynamic" keyword on both "functions and properties". Now that you made your properties use dynamic dispatching, you know what is happening behind the scene?, this dynamic property creates two function first one is getter && second one is setter whose task is to obtain && change the value of the properties.
For example, let the properties that uses dynamic dispatching are:
"dynamic var variable1: Float = 0.0"
"dynamic var variable2: Float = 0.0"
The function that that will observe the change in the properties is:
"addObserver(_:,forKeyPath:, options:, context:)"
For above properties this function looks like:
"addObserver(self, forKeyPath: "variable1", options: .allZeros, context: nil)"
"addObserver(self, forKeyPath: "variable2", options: .allZeros, context: nil)"
"override func observeValueForKeyPath(ketPath_:,ofObject:,change:,context:)"
According to my properties here is the method that will do:
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
switch keyPath {
case "variable1":
println("variable1 changed")
case "variable2":
println("variable2 changed")
default:
break
}
Thats how Key Value Observing with Dynamic Dispatching is Implemented.