How to get the values of a dictionary in Swift?
Overview
A dictionary is made up of key-value pairs. The values property in Swift only returns the dictionary’s values without returning the keys.
Syntax
dictionary.values
Return value
The value returned is a collection containing the values of the Dictionary instance dictionary.
Code
// create a dictionaryvar phones = ["S" : "Samsung","I" : "Iphone","G" : "Gionee","T" : "Tecno"]// get only the values of the dictionaryprint(phones.values)
Explanation
- Line 2–6: We create a dictionary called
phonesand populate it with some elements. - Line 10: We get the dictionary’s values using the
valuesproperty. The values are then printed to the console.