我尝试按照KeyPath<Object, Value>
这样获取价值
func myFunc<Object: NSObject, Value>(in object: Object, for keyPath: KeyPath<Object, Value>) {
let value = object[keyPath: keyPath]
debugPrint(value, type(of: value)) // correct printing value
print("nil? = ", value == nil) // always fail ??
print("notNil? = ", value != nil) // always notNil ??
}
我需要将value
与nil
进行比较
但是我在控制台中得到了非常奇怪的结果,我的检查总是有notNil
结果
的例子
var test1: String! = "test"
myObservation.myFunc(in: self, for: \.test1)
控制台输出:
测试Swift.ImplicitlyUnwrappedOptional
零? = false
notNil? = true
的例子
var test2: String? = nil
myObservation.myFunc(in: self, for: \.test2)
控制台输出:
nil Swift.Optional
零? = false // ???
notNil? = true
如何将value
与nil进行比较?