可选值上 KeyPath 的奇怪错误

Strange error with KeyPath on optional value

考虑以下代码片段

class A {
    
    var value: Int?
    
}

let a: A? = A()
let kp = \A.value
a?[keyPath: kp] = 10
print(a?.value)

这完美地工作并且 Optional(10) 按预期打印。在我的实际应用程序中,我试图以这种方式设置的字段被声明为 Date? 并且它会导致一些奇怪的错误。我实际应用的 MWE 是这样的:

class A {
    
    var value: Date?
    
}

let a: A! = A()
let kp = \A.value
a?[keyPath: kp] = Date() // Line with error
print(a?.value)

但是编译器抱怨突出显示的行并说:

Value of optional type 'Date?' must be unwrapped to a value of type 'Date'

Fix: Coalesce using '??' to provide a default when the optional value contains 'nil'

Fix: Force-unwrap using '!' to abort execution if the optional value contains 'nil'

这是我们期望在最终版本之前修复的编译器错误,还是我对键路径不了解?

我正在使用 Xcode 11 beta 3,但我在 beta 2 中也遇到了同样的问题。如果它有用,实际代码是 here

写这个没有!和 ?作品

let a = A()
let kp = \A.value
a[keyPath: kp] = Date() 
print(a.value)

或仅作为可选

let a: A? = A()
let kp = \A.value
a?[keyPath: kp] = Date() 
print(a?.value)

我遇到了类似的问题:

import Foundation

struct Outer {
    var inner: Inner

    init() {
        inner = Inner()
    }
}

struct Inner {
    var date: Date?
}

var outer = Outer()
outer[keyPath: \Outer.inner][keyPath: \Inner.date] = Date() // Line with error
print(outer.inner.date)
error: value of optional type 'Date?' must be unwrapped to a value of type 'Date'
outer[keyPath: \Outer.inner][keyPath: \Inner.date] = Date() // Line with error
                            ^
note: coalesce using '??' to provide a default when the optional value contains 'nil'
outer[keyPath: \Outer.inner][keyPath: \Inner.date] = Date() // Line with error
                            ^
                                                   ?? <#default value#>
note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
outer[keyPath: \Outer.inner][keyPath: \Inner.date] = Date() // Line with error
                            ^
                                                  !

奇怪的是,将值转换为可选的作品

outer[keyPath: \Outer.inner][keyPath: \Inner.date] = Date() as Date? // Works

因此,我认为解决此问题的更通用的解决方法是将值显式转换为可选 as Date?