我收到[String:Any]类型的Dictionary,我想检查字典中的值是否为符合特定协议的Array:

protocol ToDictionary {
    var badjoras: Bool { get set }
}

struct Badjoras: ToDictionary {
    var badjoras: Bool
}

let newArray: [String: Any] = ["First": [Badjoras(badjoras: true)]]

for (key, value) in newArray {
    if let newValue = value as? [ToDictionary] {
        print(true)
    }
}


这在Swift 3.0中完美地工作,但是在Swift 2.2中则没有。关于如何实现这一目标的任何想法?

谢谢

最佳答案

尝试以下:

protocol ToDictionary {
    var badjoras: Bool { get set }
}

struct Badjoras: ToDictionary {
    var badjoras: Bool
}

let newArray: [String: Any] = ["First": [Badjoras(badjoras: true)]]

for (key, value) in newArray {
    if value is [ToDictionary] {
        print(true)
    }
}

关于arrays - Swift 2.2阵列符合协议(protocol),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40018433/

10-10 14:39