I'm having some trouble getting the new KVO syntax right. According to the Apple documentation:
Create an observer for the key path and call the observe(_:options:changeHandler) method. For more information on key paths, see Keys and Key Paths.
class MyObserver: NSObject {
@objc var objectToObserve: MyObjectToObserve
var observation: NSKeyValueObservation?
init(object: MyObjectToObserve) {
objectToObserve = object
super.init()
observation = observe(\.objectToObserve.myDate) { object, change in
print("Observed a change to \(object.objectToObserve).myDate, updated to: \(object.objectToObserve.myDate)")
}
}
}
let observed = MyObjectToObserve()
let observer = MyObserver(object: observed)
observed.updateDate()
I'm initializing my observation like so:
self.observation = self.webView!.observe(\.webView.isLoading, changeHandler: { (webView, observedChange) in
//code
})
but am getting this error:
KVO, which stands for Key-Value Observing, is one of the techniques for observing the program state changes available in Objective-C and Swift. The concept is simple: when we have an object with some instance variables, KVO allows other objects to establish surveillance on changes for any of those instance variables.
Key-value observing is a Cocoa programming pattern you use to notify objects about changes to properties of other objects. It's useful for communicating changes between logically separated parts of your app—such as between models and views. You can only use key-value observing with classes that inherit from NSObject .
Swift property observers For example, you could add a @WrappedDefault property to a view controller. To make use of the property observers within a centralized class like AppSettings , you would need to pass in a closure to execute in the observers or post a Notification — neither of which are ideal.
Turns out the syntax needs to be like this, using the object Type rather than the object instance name:
self.observation = self.webView!.observe(\WKWebView.isLoading, changeHandler: { (webView, observedChange) in
//code
})
Misread the documentation ¯\_(ツ)_/¯
If you use the \.
syntax the root element is the observed object so it's simply
self.observation = self.webView!.observe(\.isLoading, ...
The compiler treats your syntax as webView.webView.isLoading
which is obviously not intended.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With