I have an array of Person
\'s objects:
class Person {
let name:String
let position:Int
}
and the array is:
You may write custom initializer for Dictionary
type, for example from tuples:
extension Dictionary {
public init(keyValuePairs: [(Key, Value)]) {
self.init()
for pair in keyValuePairs {
self[pair.0] = pair.1
}
}
}
and then use map
for your array of Person
:
var myDictionary = Dictionary(keyValuePairs: myArray.map{($0.position, $0.name)})