Home ios kvo
Post
Cancel

ios kvo

Q & A

1
2
3
4
5
6
observation = observe(
                    \.textView.contentSize,
                    options: [.new]
                ) {[unowned self] object, change in

                }

Fatal error: Could not extract a String from KeyPath Swift.ReferenceWritableKeyPath<xxxx.xxxxxxTextViewDelegate, __C.CGSize>

Note the @objc keyword

References

https://developer.apple.com/documentation/swift/using-key-value-observing-in-swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class MyObserver: NSObject {
    @objc var objectToObserve: MyObjectToObserve
    var observation: NSKeyValueObservation?

    init(object: MyObjectToObserve) {
        objectToObserve = object
        super.init()

        observation = observe(
            \.objectToObserve.myDate,
            options: [.old, .new]
        ) { object, change in
            print("myDate changed from: \(change.oldValue!), updated to: \(change.newValue!)")
        }
    }
}
This post is licensed under CC BY 4.0 by the author.