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 dictionary
var phones = [
"S" : "Samsung",
"I" : "Iphone",
"G" : "Gionee",
"T" : "Tecno"
]
// get only the values of the dictionary
print(phones.values)

Explanation

  • Line 2–6: We create a dictionary called phones and populate it with some elements.
  • Line 10: We get the dictionary’s values using the values property. The values are then printed to the console.

Free Resources