在 swift 2.0 中玩反射我试图键入检查子值。
问题:任何项目的镜像中子数组的每个元素都不是可选的,但他的类型可以是可选的......发生的情况当然是我有子值,即使值为零
也许不清楚,所以我在这里放了一些代码来更好地解释。
为方便起见,我在 Mirror 扩展中定义了一个下标,用于获取具有给定标签的子对象
extension Mirror {
public subscript(key: String)->Child?{
var child = children.filter {
var valid = false
if let label = $0.label {
valid = label == key
}
return valid
}.last
if child == nil,
let superMirror = superclassMirror() {
child = superMirror[key]
}
return child
}
}
完美,现在假设我有这门课
class Rule: NSObject, AProtocol {
var hello: String?
var subRule: Rule?
}
好的,现在问题来了
let aRule = Rule()
let mirroredRule = Mirror(reflecting:aRule)
if let child = mirroredRule["subRule"] {
//child.value always exists
//i can't do child.value is AProtocol? because child.value is not optional
//child.value is AProtocol of course returns false
//child.dynamicType is Optional(Rule)
if let unwrapped = unwrap(child.value) where unwrapped is AProtocol {
//This of course works only if child.value is not nil
//so the unwrap function returns an unwrapped value
//this is not a definitive solution
}
}
child.value 尚未初始化,因此为 nil,我无法使用 unwrap 函数检查他的类型。我正在编写一个反序列化器,所以我还需要检查 var 是否为零,因为在将用于反序列化的字典中可以定义它。
private func unwrap(subject: Any) -> Any? {
var value: Any?
let mirrored = Mirror(reflecting:subject)
if mirrored.displayStyle != .Optional {
value = subject
} else if let firstChild = mirrored.children.first {
value = firstChild.value
}
return value
}
我希望问题很清楚。有什么建议?
最佳答案
基于 this answer , 我推荐使用 if case Optional<Any>.some(_)
.
我最近做了一些事情来确保我的结构上至少有一个可选集。您可以粘贴到操场上:
struct ErrorResponse: Codable {
let message: String?
let authorizationException: [String: String]?
let validationException: String?
let generalException: String?
var isValid: Bool {
var hasAtLeastOneNonNilErrorValue = false
Mirror(reflecting: self).children.forEach {
if case Optional<Any>.some(_) = $0.value {
hasAtLeastOneNonNilErrorValue = true
}
}
return hasAtLeastOneNonNilErrorValue
}
}
let errorTest = ErrorResponse(message: "some message", authorizationException: nil, validationException: nil, generalException: nil)
let errorTest2 = ErrorResponse(message: nil, authorizationException: nil, validationException: nil, generalException: nil)
print("is valid: \(errorTest.isValid)") //is valid: true
print("is valid: \(errorTest2.isValid)") //is valid: false
关于选项的反射和类型检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31970553/