map dictionary to array in swift

You can map a dictionary to an array in Swift using the map() function on the dictionary's values. Here's an example:

main.swift
let dictionary = ["apple": 1, "banana": 2, "orange": 3]

let array = dictionary.values.map { $0 }

print(array) // [1, 2, 3]
125 chars
6 lines

In the example above, we have a dictionary with string keys and integer values. We use the values property of the dictionary to access an array of its values, and then use the map() function on that array to transform each value into itself. This effectively maps each value in the dictionary to a corresponding value in the resulting array.

gistlibby LogSnag