find the values of a map in swift

To obtain the values of a dictionary (or map), you can use the values property of the dictionary. The values property returns an array of the values of the dictionary. Here's an example of obtaining the values of a dictionary in Swift:

main.swift
let myDict = ["apple": 3, "orange": 7, "banana": 2]
let values = Array(myDict.values)
print(values) // [3, 7, 2]
113 chars
4 lines

In the example above, myDict is a dictionary with keys "apple", "orange", and "banana", mapped to their respective values 3, 7, and 2. To obtain an array of just the values, we call the values property of the dictionary and convert it to an array using the Array initializer. Finally, we print out the resulting array of values.

Note that the order of the values in the resulting array is not necessarily the same as the original order of the key-value pairs in the dictionary. In fact, the order may change with each execution of the program. If you need to maintain the order, you should consider using an array of tuples instead of a dictionary.

gistlibby LogSnag