.values

Anonymous contributor's avatar
Anonymous contributor
Published Sep 18, 2024
Contribute to Docs

The .values property in Swift is used to access a collection of all the values stored in a key-value dictionary.

Syntax

dictionaryName.values
  • dictionaryName: Refers to the dictionary instance from which the values are accessed.

Example

The code below creates a dictionary with key-value pairs and then uses the .values property to print only the values from the dictionary:

var worldCupWinners = [
"Argentina": 2022,
"France": 2018,
"Germany": 2014,
"Spain": 2010,
]
print(worldCupWinners.values)

The code above generates the following output:

[2010, 2022, 2018, 2014]

Note: The values will be printed in a random order.

All contributors

Contribute to Docs

Learn Swift on Codecademy