[英]Convert an optional Type to non-optional Type
The following code throws a compile error:以下代码引发编译错误:
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
This is due to the fact that type(of: obj[keyPath: \MyObj.nestedObj])
is NestedObject?
这是因为
type(of: obj[keyPath: \MyObj.nestedObj])
是NestedObject?
, which of course cannot be initialized. ,当然不能初始化。 I need a way to unwrap the optional typing and get the non-optional typing
NestedObject
.我需要一种方法来解开可选类型并获取非可选类型
NestedObject
。
Assumptions that have to be maintained.必须维持的假设。
nestedObj
to be nil.nestedObj
有可能为零。NestedObject
is.NestedObject
的类型是什么。 It could be any type, but I am assured those types are all Initable
.Initable
。nestedObject
via a keypath.nestedObject
的属性。nestedObject
is nil, I need to create a new NestedObject
and set the value via a keypath.nestedObject
为nil,我需要创建一个新的NestedObject
并通过keypath 设置值。 This is a small part of a larger system, so the above assumptions must be considered.这是较大系统的一小部分,因此必须考虑上述假设。 I cannot change those.
我无法改变那些。
You can use your optional Wrapped
type to initialize your nested object:您可以使用可选的
Wrapped
类型来初始化嵌套的 object:
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!!"
If you want the resulting object to be optional as well:如果您希望生成的 object 也是可选的:
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!!"
You need to add those helpers:您需要添加这些助手:
protocol AnyOptional {
associatedtype Wrapped
var optional: Optional<Wrapped> { get }
}
extension Optional: AnyOptional {
var optional: Optional<Wrapped> { self }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.