I read that NSObject.observe<Value>()
is the new API (introduced in Swift 4) for registering a KVO observer and it supports Swift keypath expression and closure. I wonder what's the approach in the new API to unregister an observer?
The observe<Value>()
method returns an NSKeyValueObservation
object, which doesn't have a method to cancel the observation explicitly (according to its code it does that implicitly when the object is deallocated by ARC). So it seems the only approach is to use the old NSObject.removeObserver()
which uses the old keypath String parameter?
I also find it's hard to find documents for this observe()
API. There is no mention of it in NSObject
doc (the doc contains description of observeValue()
but not this observe<Value>
), and code completion in XCode doesn't work with it. The only place I found it was mentioned in Apple official doc is this article Using Key-Value Observing in Swift. That leads me to think if this is incomplete or ongoing work? (But given it was introduced in Swift 4 time frame, I doubt it).