如何将其中包含 nil 的 Any 值转换为 Any? [英] How do I cast an Any value with nil in it to a Any?

查看:88
本文介绍了如何将其中包含 nil 的 Any 值转换为 Any?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用反射来尝试检查结构是否具有 nil 值.

I am using reflection to try to check if a struct has a nil value.

struct MyStruct {
    let myString: String?
}

let properties = Mirror(reflecting: MyStruct(myString: nil)).children.filter { $0.label != nil }

for property in properties {
    if property.value == nil { // value has type "Any" will always fail.
        print("property \(property.label!) is nil")
    }
}

如何将 Any 类型转换为 Any?

How do I cast the Any type to Any?

推荐答案

要简单地检查包裹在 Any 中的属性值中的 nil 内容,您可以,与其他答案中描述的方法相反,实际上可以通过将模式匹配直接应用于 Optional< 来解决对具体的非 Any 类型进行强制转换/绑定/检查的工作.Any>.noneOptional.some(...).

To simply check for nil content in the property value wrapped in an Any, you can, contrary to methods described in the other answers, actually work your way around casting/binding/checking to a concrete non-Any type by directly applying pattern matching to Optional<Any>.none or Optional<Any>.some(...).

示例设置(不同的成员类型:我们不想仅仅为了对nil 内容进行反射检查而对所有这些不同类型进行注释)

Example setup (different members types: we don't want to annotate all these different types simply for reflection checking for nil content)

struct MyStruct {
    let myString: String?
    let myInt: Int?
    let myDouble: Double?
    // ...
    init(_ myString: String?, _ myInt: Int?, _ myDouble: Double?) {
        self.myString = myString
        self.myInt = myInt
        self.myDouble = myDouble
    }
}

简单日志记录:提取nil值属性的属性名称

Simple logging: Extracting property names of nil valued properties

模式匹配Optional.none,如果你只是想记录nil值实体的信息:

Pattern matching to Optional<Any>.none, if you simply want to log info on nil valued entities:

for case (let label as String, Optional<Any>.none) in 
    Mirror(reflecting: MyStruct("foo", nil, 4.2)).children {
    print("property \(label) is nil")
}
/* property myInt is nil */

更详细的日志记录:对于nil 以及非nil 值的属性

Slightly more detailed logging: for nil as well as non-nil valued properties

模式匹配到 Optional.some(...),以防您想要更详细的日志记录(下面绑定的 x 值对应于您保证的非-nil Any 实例)

Pattern matching to Optional<Any>.some(...), in case you want more detailed logging (the binded x value below corresponds to your guaranteed non-nil Any instance)

for property in Mirror(reflecting: MyStruct("foo", nil, 4.2)).children {
    if let label = property.label {
        if case Optional<Any>.some(let x) = property.value {
            print("property \(label) is not nil (value: \(x))")
        }
        else {
            print("property \(label) is nil")
        }
    }
}
/* property myString is not nil (value: foo)
   property myInt is nil
   property myDouble is not nil (value: 4.2) */

或者,后者使用 switch 来代替:

Or, the latter using a switch case instead:

for property in Mirror(reflecting: MyStruct("foo", nil, 4.2)).children {
    switch(property) {
        case (let label as String, Optional<Any>.some(let x)): 
            print("property \(label) is not nil (value: \(x))")
        case (let label as String, _): print("property \(label) is nil")
        default: ()
    }
}
/* property myString is not nil (value: foo)
   property myInt is nil
   property myDouble is not nil (value: 4.2) */

这篇关于如何将其中包含 nil 的 Any 值转换为 Any?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆