Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何将包含nil的Any值转换为Any?_Swift_Reflection - Fatal编程技术网

Swift 如何将包含nil的Any值转换为Any?

Swift 如何将包含nil的Any值转换为Any?,swift,reflection,Swift,Reflection,我正在使用反射来检查结构是否具有nil值 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"

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

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?

我们可以使用
String(description:value)
value as避免强制转换为
String
?字符串
,直接测试其类型

for (label, value) in properties {
  if !(value is String) {
    print("Property \(label!) is nil")
  }
}

我们可以避免使用
字符串(描述:value)
值作为?字符串
,直接测试其类型

for (label, value) in properties {
  if !(value is String) {
    print("Property \(label!) is nil")
  }
}

您不能将
Any
强制转换为
Any?
类型。不幸的是,
Any
无法与
nil
进行比较,因为它实际上保存的是enum
Optional
,后者保存的是
nil

与其尝试将
Any
强制转换为可选类型
Any?
,不如将
Any
中已有的可选类型强制转换为
String
类型。应将可选绑定与类型转换一起使用:

for property in properties {
    if let value = property.value as? String {
        print ("property \(property.label!) is not nil");
    } else {
        print ("property \(property.label!) is nil");
    }
}
Any
类型可以保存任何内容,包括optionals,因为它们实际上是一个枚举类型(
可选的
),它计算
case none
case some(T)
。因此
nil
实际上就是其中一种情况:

nil == Optional<T>.none // Always returns true
nil==Optional.none//始终返回true

因此,与其将
Any
类型评估为
nil
,不如尝试可选绑定。如果成功,它没有
nil
,如果没有,它确实有
nil

您不能将
Any
强制转换为
Any?
类型。不幸的是,
Any
无法与
nil
进行比较,因为它实际上保存的是enum
Optional
,后者保存的是
nil

与其尝试将
Any
强制转换为可选类型
Any?
,不如将
Any
中已有的可选类型强制转换为
String
类型。应将可选绑定与类型转换一起使用:

for property in properties {
    if let value = property.value as? String {
        print ("property \(property.label!) is not nil");
    } else {
        print ("property \(property.label!) is nil");
    }
}
Any
类型可以保存任何内容,包括optionals,因为它们实际上是一个枚举类型(
可选的
),它计算
case none
case some(T)
。因此
nil
实际上就是其中一种情况:

nil == Optional<T>.none // Always returns true
nil==Optional.none//始终返回true

因此,与其将
Any
类型评估为
nil
,不如尝试可选绑定。如果成功,它没有
nil
,如果没有,它确实有
nil

,只需检查
nil
属性值中的
Any
内容,与其他答案中描述的方法相反,实际上,通过直接将模式匹配应用于
可选。none
可选。一些(…)
,您可以在铸造/绑定/检查具体的非
任何
类型方面找到方法

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

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

模式匹配到
可选。无
,如果您只想在
值实体上记录信息:

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 */
或者,后者使用
开关
外壳:

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) */
镜像中的属性(反射:MyStruct(“foo”,nil,4.2))。子对象{ 交换机(属性){ 大小写(让标签作为字符串,可选。一些(让x)): 打印(“属性\(标签)不是零(值:\(x))”) 大小写(将标签设为字符串):打印(“属性\(标签)为零”) 默认值:() } } /*属性myString不是nil(值:foo) 属性myInt为零 属性myDouble不是nil(值:4.2)*/
要简单地检查
Any
中包装的属性值中的
nil
内容,您可以,与其他答案中描述的方法相反,实际上,通过直接将模式匹配应用于
可选。none
可选。一些(…)
,您可以在铸造/绑定/检查具体的非
任何
类型方面找到方法

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

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

模式匹配到
可选。无
,如果您只想在
值实体上记录信息:

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 */
或者,后者使用
开关
外壳:

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) */
镜像中的属性(反射:MyStruct(“foo”,nil,4.2))。子对象{ 交换机(属性){ 大小写(让标签作为字符串,可选。一些(让x)): 打印(“属性\(标签)不是零(值:\(x))”) 大小写(将标签设为字符串):打印(“属性\(标签)为零”) 默认值:() } } /*属性myString不是nil(值:foo) 属性myInt为零 属性myDouble不是nil(值:4.2)*/
我知道这不是最好的答案。但它可以解决你的问题

正如prodevmx所说

nil==可选。无//始终返回true

所以你不能只是检查一下

if let value = property.value as? Any  {
}
此条件始终通过,并且“Any”不是可选值

请使用此功能

func checkAnyContainsNil(object : Any) -> Bool {
    let value = "\(object)"
    if value == "nil" {
       return true
    }
    return false
 }

将对象转换为nil字符串并检查字符串是否为nil

我知道这不是最佳答案。但它可以解决你的问题

正如prodevmx所说

nil==可选。无//始终返回true

所以你不能只是检查一下

if let value = property.value as? Any  {
}
此条件始终通过,并且“Any”不是可选值

请使用此功能

func checkAnyContainsNil(object : Any) -> Bool {
    let value = "\(object)"
    if value == "nil" {
       return true
    }
    return false
 }
将对象转换为nil字符串并检查字符串是否为nil