简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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