简体   繁体   English

将可选类型转换为非可选类型

[英]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.必须维持的假设。

  • I must used optionals as there is a chance for nestedObj to be nil.我必须使用可选项,因为nestedObj有可能为零。
  • I do not know what the type NestedObject is.我不知道NestedObject的类型是什么。 It could be any type, but I am assured those types are all Initable .它可以是任何类型,但我确信这些类型都是Initable
  • I need to be able to set a property of my nestedObject via a keypath.我需要能够通过 keypath 设置我的nestedObject的属性。
  • If 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM