December 2018
Intermediate to advanced
414 pages
10h 19m
English
As mentioned before, KVO leverages Objective-C's dynamism, which means it doesn't work on structs.
In order to use KVO with your own Swift objects, you have to make them dynamic and compatible with Objective-C using the @objc annotation:
class MyObject: NSObject { @objc dynamic var string: String = ""}
Now, the string member is observable, and you can use the following code:
let object = MyObject()var changed = falselet observer = object.observe(\MyObject.string) { (obj, change) in // Do Something with the new value changed = true}object.string = "This will emit an event"assert(changed) // ensure changed was calledobserver.invalidate() // Always invalidate!
With this technique, it is possible to listen to multiple changes from ...
Read now
Unlock full access