Swift
Dictionary
Array
Programming
iOS Development

Swift Dictionary Get values as array

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Getting the values from a Swift dictionary as an array is straightforward, but the result has two important properties: it is derived from an unordered collection, and it may need further transformation before it is useful. The core operation is simple, but understanding what kind of array you are getting matters for real application code.

Convert values to an Array

A Swift dictionary exposes a values view. To turn that collection into a real array, pass it to the Array initializer.

swift
1import Foundation
2
3let fruitColors: [String: String] = [
4    "apple": "red",
5    "banana": "yellow",
6    "grape": "purple"
7]
8
9let values = Array(fruitColors.values)
10print(values)

The type of values here is [String]. This is the standard answer to the question.

The reason the conversion exists at all is that Dictionary.Values is a specialized collection view, not a standalone array. Many APIs work with any collection, but if you need array-specific operations or storage, converting explicitly is the right move.

Remember That Dictionary Order Is Not the Contract

The biggest conceptual trap is assuming the resulting array has a stable semantic order. A dictionary is keyed storage, not an ordered list. Even if the output looks consistent in one run, you should not build logic that depends on a specific value order unless you sort it yourself.

swift
1import Foundation
2
3let scores = [
4    "Ava": 82,
5    "Ben": 91,
6    "Cara": 88
7]
8
9let rawValues = Array(scores.values)
10let sortedValues = scores.keys.sorted().compactMap { scores[$0] }
11
12print(rawValues)
13print(sortedValues)

In the second line, the values are ordered by their corresponding sorted keys. That is the better pattern when order has meaning.

Transform Values After Extraction

Once the values are in an array, normal array operations apply. That is often the real goal rather than the conversion itself.

swift
1import Foundation
2
3let prices = [
4    "apple": 1.2,
5    "banana": 0.8,
6    "orange": 1.5
7]
8
9let rounded = Array(prices.values).map { value in
10    String(format: "%.0f", value)
11}
12
13print(rounded)

You can also filter or reduce immediately:

swift
1let expensive = Array(prices.values).filter { $0 > 1.0 }
2let total = Array(prices.values).reduce(0, +)
3
4print(expensive)
5print(total)

If you only need one chained operation, you do not always have to materialize the array first. But when you need to pass the values into an API that expects [Value], explicit conversion keeps the code honest.

Optional Values and Nested Optionals

If the dictionary value type is optional, converting values directly gives an array of optionals. In that case, compactMap is often the right follow-up step.

swift
1import Foundation
2
3let phoneNumbers: [String: String?] = [
4    "home": "555-0100",
5    "work": nil,
6    "mobile": "555-9999"
7]
8
9let nonNilNumbers = phoneNumbers.values.compactMap { $0 }
10print(nonNilNumbers)

That produces a [String] with nil entries removed.

Sometimes people ask for values as an array when they really need ordered key-value pairs. If the relationship matters, do not split them apart too early.

swift
1let pairs = fruitColors.sorted { lhs, rhs in
2    lhs.key < rhs.key
3}
4
5for pair in pairs {
6    print(pair.key, pair.value)
7}

This is better than extracting only the values if later logic still needs to know which key produced which value.

Common Pitfalls

Assuming the array of values has a meaningful or guaranteed order is the most common mistake. Sort by key or by value if order matters.

Converting to an array too early can lose useful key context. Keep pairs together if downstream logic still depends on the original mapping.

For optional value dictionaries, forgetting to use compactMap leaves you with an array of optionals when you may actually want plain values.

Summary

  • Use Array(dictionary.values) to convert dictionary values into a Swift array.
  • Do not rely on a dictionary's natural value order unless you sort explicitly.
  • Use map, filter, reduce, or compactMap after extraction depending on the next operation.
  • Keep key-value pairs together when the association still matters.

Course illustration
Course illustration

All Rights Reserved.