Swift Dictionary values

The values property returns just the values of the dictionary.

Example

var languages = ["Swift": 2012, "C": 1972, "Java": 1995]

// return just the values of languages var result = languages.values
print(result) // Output: [1995, 2012, 1972]

values Syntax

The syntax of the dictionary values property is:

dictionary.values 

Here, dictionary is an object of the Dictionary class.


values Return Values

The values property returns an array of all the values of dictionary.


Example 1: Swift Dictionary values

var information = ["Alcaraz": 18, "Sinner": 20, "Nadal": 34]

// values total elements on names print(information.values)

Output

[18, 20, 34]

In the above example, we have used the values property to return an array of just the values of the information dictionary.


Example 2: Using values With for Loop

var informations = ["Alcaraz": 18, "Sinner": 20, "Nadal": 34]

// iterate through all the values of informations for information in informations.values {
print(information) }

Output

20
34
18

Here, we have used the for loop to iterate through all the values of the informations dictionary.

Finally, all the values of informations are printed one at a time.

Did you find this article helpful?