在Swift中,将两个数组分配给字典的键/值的最佳方法是什么?

时间:2014-06-06 08:30:26

标签: dictionary swift

假设我有两个数组:

let myKeys = ["one", "two", "three"]
let myValues = ["1",  "2", "3"]

和空字典:

var myDictionary = Dictionary<String, String>()

分别将myKeysmyValues分配为myDictionary的键和值的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

据我所知,这是为词典分配值/键的方法:

let count = myKeys.count
let count2 = myValues.count
if (count == count2) {
    for index in 0..count {
        myDictionary.updateValue(myValues[index], forKey:myKeys[index])
    }
}

编辑:Swift 2

let count = myKeys.count
let count2 = myValues.count
if (count == count2) {
    for index in 0 ..< count {
        myDictionary.updateValue(myValues[index], forKey:myKeys[index])
    }
}