假设我有两个数组:
let myKeys = ["one", "two", "three"]
let myValues = ["1", "2", "3"]
和空字典:
var myDictionary = Dictionary<String, String>()
分别将myKeys
和myValues
分配为myDictionary
的键和值的最佳方法是什么?
答案 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])
}
}