+keyPathsForValuesAffecting<Key> in Swift 4 won't trigger a KVO callback in <Key> when the specified keypaths change

Originator:oscar
Number:rdar://32700827 Date Originated:11/06/2017
Status:Open Resolved:Duplicate of 32593787
Product:Swift Product Version:4
Classification:Bug Reproducible:Always
 
Steps to Reproduce:
Download the project "KVOObservationTestSwift" project attached and open it in Xcode 9, run it in the iOS simulator.

MyObject specifies "isReady" as a keyPath whose change affects `\MyObject.name`. Click the "Button" button on the simulator. That button tap flips the value of `isReady` on a `MyObject` instance. This should trigger a KVO callback on ViewController, but it doesn't.

Expected Results:
Changing values on keypaths specified by the class methods keyPathsForValuesAffecting<Key> on Swift 4 should actually trigger KVO notifications for <Key> when the returned keypaths are changed.

Observed Results:
Keypaths defined in keyPathsForValuesAffecting<Key> methods on Swift 4 do not trigger a change on <Key> when the returned keypaths are changed.

Version:
Xcode Version 9.0 beta (9M136h)
Swift 4

Notes:
If instead of using + keyPathsForValuesAffecting<Key> you return the keypaths from the keyPathsForValuesAffectingValue(forKey:) method, it works correctly.

Attached you can find test projects in Swift and Objective-C.

Comments

Beware @objc *and* dynamic for the (Swift) properties in keypaths

Just to put it here (it's probably unrelated to the OP problem as in my case keyPathsForValuesAffectingValue(forKey:) also didn't work (even though both were clearly invoked on KVO registration)). You must use '@objc dynamic' for (Swift) properties in the keypaths. The notifications won't be triggered for such properties if they are declared just with "@objc".

By Grigory.Entin at March 25, 2018, 9:16 p.m. (reply...)

Is the original issue (32593787) resolved? I can't seem to be able to access it.

The function needs to be marked @objc

Yeah, they responded to me. There still seems to be an inconsistency with how it works. It's been marked as a duplicate of 32593787.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!