Swift 4 Generic KVO API sends nil change object

Originator:daltonclaybrook
Number:rdar://42967394 Date Originated:8/6/2018
Status:Open Resolved:
Product:Swift Product Version:4
Classification:Serious Bug Reproducible:Always
 
Summary:
Swift 4 introduced a new Key-Value Observing API which uses generic KeyPath objects in order to work better with the Swift type system. These APIs seem to work fine for observing objects and primitives, but if you observe an Objective-C enum, the NSKeyValueObservedChange object always contains nil values.

Steps to Reproduce:
Create an observation on a KVO-compliant Objective-C enum using the new Swift KVO API. One such example is AVPlayer.timeControlStatus, which is a AVPlayerTimeControlStatus. In the change handler closure, try to unwrap the timeControlStatus.

    let observation = player.observe(\AVPlayer.timeControlStatus, options: [.initial, .new], changeHandler: { (player: AVPlayer, change: NSKeyValueObservedChange<AVPlayerTimeControlStatus>) in
      if let newStatus = change.newValue {
        // handle new
      } else if let oldStatus = change.oldValue {
        // handle old
      } else {
        // neither value is present
      }
    })

Expected Results:
When the value of the KVO-compliant property changes and the change handler is executed, `change.newValue` should be non-nil.

Actual Results:
When the value of the KVO-compliant property changes and the change handler is executed, `change.newValue` is nil.

Version/Build:
Xcode 9.4.1 (9F2000) and Swift 4.1

Comments


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!