Let’s say I have array of objects that can be identified and I want to create dictionary from it. I can easily get tuples from my array like so:
let tuples = myArray.map { return ($0.id, $0) }
But I can’t see initializer for dictionary to take array of tuples. Am I missing something? Do I have create extension for dictionary for this functionality (in fact it’s not hard but I thought it would be supplied by default) or there is easier way to do that?
There is code for extension
extension Dictionary
{
public init (_ arrayOfTuples : Array<(Key, Value)>)
{
self.init(minimumCapacity: arrayOfTuples.count)
for tuple in arrayOfTuples
{
self[tuple.0] = tuple.1
}
}
}