Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dictionary 在Swift中,为字典的键/值分配两个数组的最佳方法是什么?_Dictionary_Swift - Fatal编程技术网

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。