我使用反射来尝试检查结构是否具有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")
}
}
Run Code Online (Sandbox Code Playgroud)
如何将Any类型转换为Any?