Initialize dictionary from array with Swift 3

Is there an easier / more efficient way to initialize a dictionary from an array in Swift 3?

for object in array {
    dict[object.id] = object
}

      

Nothing wrong with that, but I was wondering if you can use map / reduce / etc to do this with a little less code.

+3


source to share


1 answer


array.forEach({ object in
    dict[object.id] = object
})

      

or shorter:



array.forEach({ dict[$0.id] = $0 })

      

+4


source







All Articles