数组和字典,循环的正确语法是什么?

时间:2016-10-07 18:32:30

标签: arrays swift loops dictionary

我在array dictionary中遇到循环问题。我需要这样的东西。如何检查字符串数组的项是否等于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

            }
        }
    }

这个循环的正确语法是什么?

1 个答案:

答案 0 :(得分:2)

这样迭代:

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 {

            }

        }
    }
}