问题描述
import UIKit
class Initable {
required init() {}
}
class nestedobject:Initable {
var nestedProp:String? = nil
}
class MyObj {
var name:String? = nil
var value:Int? = nil
var otherVals:[String]? = nil
var nestedobj:nestedobject? = nil
}
let obj = MyObj()
var nestedobj = obj[keyPath: \MyObj.nestedobj]
if(nestedobj == nil) {
nestedobj = type(of: obj[keyPath: \MyObj.nestedobj]).init()
}
nestedobj![keyPath: \nestedobject.nestedProp] = "nesT ME BABY!!"
obj[keyPath: \MyObj.nestedobj] = nestedobj!
obj
这是因为type(of: obj[keyPath: \MyObj.nestedobj])
是nestedobject?
,当然不能被初始化。我需要一种方法来解开可选类型并获取非可选类型 nestedobject
。
必须保持的假设。
- 我必须使用可选项,因为
nestedobj
有可能为零。 - 我不知道
nestedobject
是什么类型。它可以是任何类型,但我确信这些类型都是Initable
。 - 我需要能够通过键路径设置我的
nestedobject
的属性。 - 如果
nestedobject
为零,我需要创建一个新的nestedobject
并通过键路径设置值。
这是更大系统的一小部分,因此必须考虑上述假设。我无法改变那些。
解决方法
您可以使用可选的 Wrapped
类型来初始化您的嵌套对象:
let obj = MyObj()
let nestedObj = type(of: obj[keyPath: \MyObj.nestedObj]).Wrapped()
nestedObj[keyPath: \NestedObject.nestedProp] = "NEST ME BABY!!"
print(nestedObj.nestedProp ?? "") // "NEST ME BABY!!"
如果您希望结果对象也是可选的:
let obj = MyObj()
var nestedObj = obj[keyPath: \MyObj.nestedObj]
if nestedObj == nil {
nestedObj = type(of: obj[keyPath: \MyObj.nestedObj]).Wrapped()
}
nestedObj?[keyPath: \NestedObject.nestedProp] = "NEST ME BABY!!"
您需要添加这些助手:
protocol AnyOptional {
associatedtype Wrapped
var optional: Optional<Wrapped> { get }
}
extension Optional: AnyOptional {
var optional: Optional<Wrapped> { self }
}