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

标签 dictionary swift

假设我有两个数组:

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

和空字典:

var myDictionary = Dictionary<String, String>()

myKeysmyValues 分别指定为 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/

相关文章:

python - 如何在Python中编写一个分割字符串并将其转换为字典的函数

使用 Sorted 对 Python 字典进行排序

ios - 代码 -6003 是否有类似 NSURLError 枚举的常量?

php - iOS 应用程序中的服务器保护

swift - 如何在不创建新记录的情况下更新cloudKit上的数据?

python - 对包含列表作为值的字典列表进行排序

html - map 多边形周围的黑色轮廓 (IE)

javascript - 如何获取 JavaScript 字典中的键集合?

regex - 如何在 Swift 中用 NSRegularExpression 替换大写字符串?

ios - 通过覆盖它的指定初始化器从 Storyboard 中获取 UIVIewController