Gug*_*thu 1 arrays dictionary swift
我有一个数组数组和一个整数数组,并希望创建一个字典,其中Integers数组元素作为数组数组中元素的键.
我已经尝试了许多迭代方法而没有太多运气.有什么想法或想法吗?
var populationArray = [[98, 8, 45, 34, 56], [9, 13, 65, 4, 90], [24, 5, 4, 56, 88], [3, 55, 22, 19, 10], [8, 33, 26, 93, 16], [31, 38, 92, 70, 36], [9, 39, 15, 14, 66]]
var IntegerKeys = [17, 41, 10, 34, 5, 85, 87]
var Dictionary : [Int: [Int]] = [:]
Run Code Online (Sandbox Code Playgroud)
试试这个:
var populationArray = [[98, 8, 45, 34, 56], [9, 13, 65, 4, 90], [24, 5, 4, 56, 88], [3, 55, 22, 19, 10], [8, 33, 26, 93, 16], [31, 38, 92, 70, 36], [9, 39, 15, 14, 66]]
var IntegerKeys = [17, 41, 10, 34, 5, 85, 87]
var Dictionary = [Int: [Int]]()
// Swift 2.0
for (index, key) in IntegerKeys.enumerate() {
Dictionary[key] = populationArray[index]
}
// Swift 1.2
for (index, key) in enumerate(IntegerKeys) {
Dictionary[key] = populationArray[index]
}
Run Code Online (Sandbox Code Playgroud)