简体   繁体   English

Swift 2.2阵列符合协议

[英]Swift 2.2 Array conforms to protocol

I'm receiving a Dictionary of type [String: Any] and I wanna check if value in the dictionary is an Array that conform's to a certain protocol: 我收到类型为[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)
    }
}

This works perfectly in Swift 3.0, but in Swift 2.2 it does not. 这在Swift 3.0中完美地工作,但是在Swift 2.2中则不能。 Any ideias on how can I achieve this? 关于如何实现这一目标的任何想法?

Thanks 谢谢

Try Following : 尝试以下:

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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM