Dictionary 在Swift中,为字典的键/值分配两个数组的最佳方法是什么?
假设我有两个数组:Dictionary 在Swift中,为字典的键/值分配两个数组的最佳方法是什么?,dictionary,swift,Dictionary,Swift,假设我有两个数组: let myKeys = ["one", "two", "three"] let myValues = ["1", "2", "3"] 和空字典: var myDictionary = Dictionary<String, String>() let count = myKeys.count let count2 = myValues.count if (count == count2) { for index in 0..count {
let myKeys = ["one", "two", "three"]
let myValues = ["1", "2", "3"]
和空字典:
var myDictionary = Dictionary<String, String>()
let count = myKeys.count
let count2 = myValues.count
if (count == count2) {
for index in 0..count {
myDictionary.updateValue(myValues[index], forKey:myKeys[index])
}
}
var myDictionary=Dictionary()
将
myKeys
和myValues
分别指定为myDictionary
的键和值的最佳方法是什么?据我所知,这是为词典指定值/键的方法:
var myDictionary = Dictionary<String, String>()
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])
}
}
let count=myKeys.count
让count2=myValues.count
if(count==count2){
对于0中的索引..<计数{
updateValue(myValues[index],forKey:myKeys[index])
}
}
您确定不想要0..count
?但现在它只从0变为count-2。