← All languages
Swift function of the day
Random

Dictionary.values

A collection containing just the values of the dictionary.

Description

The values property returns a collection of all the values in the dictionary. The returned collection is of type Dictionary.Values, which conforms to Collection.

Like keys, the order of values is not guaranteed. You can convert to an array with Array(dict.values) or iterate directly.

Accessing the values collection is O(1). Iterating is O(n).

Example

let scores = ["Alice": 90, "Bob": 85]
let total = scores.values.reduce(0, +)
print(total)  // 175

Reference