Is there a more simple/efficient way to initialize a dictionary from an array in Swift 3?
for object in array {
dict[object.id] = object
}
Nothing wrong with the above but I was wondering if you could use map/reduce/etc to do this with slightly less code.
array.forEach({ object in
dict[object.id] = object
})
or shorter:
array.forEach({ dict[$0.id] = $0 })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With