简体   繁体   中英

Array and dictionary, what's the correct syntax of loop?

I have a problem with loop in array dictionary . I need something like this. How to check if item of string array is equal item of messagesDictionary :

var stringArray = ["first","second","third"]
var messagesDictionary = [["first": 50],["second": 60],["third": 70]]

    for item in stringArray {
        for itemDic in messagesDictionary {
            if item == itemDic[key] { // this 'itemDic[key]' wrong

            }
        }
    }

What is the correct syntax of this loop?

Iterate like this instead:

let stringArray:[String]  = ["first","second","third"]
let messagesDictionary: [[String: Any]] = [["first": 50],["second": 60],["third": 70]]
for item in stringArray {
    for itemDic in messagesDictionary {
        for (key, value) in itemDic {
            if item == key {

            }

        }
    }
}

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