假设我有两个数组:
let myKeys = ["one", "two", "three"]
let myValues = ["1", "2", "3"]
和空字典:
var myDictionary = Dictionary<String, String>()
将 myKeys
和 myValues
分别指定为 myDictionary
的键和值的最佳方法是什么?
最佳答案
据我所知,这是为字典分配值/键的方法:
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])
}
}
关于dictionary - 在 Swift 中,将两个数组分配给字典的键/值的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24077416/