Swift 如何将包含nil的Any值转换为Any?
我正在使用反射来检查结构是否具有nil值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"
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
进行比较,因为它实际上保存的是enumOptional
,后者保存的是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
进行比较,因为它实际上保存的是enumOptional
,后者保存的是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