Swift Dictionary Get values as array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
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.
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.
You can also filter or reduce immediately:
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.
That produces a [String] with nil entries removed.
When Keys and Values Must Stay Related
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.
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, orcompactMapafter extraction depending on the next operation. - Keep key-value pairs together when the association still matters.
Related reading
- Swift JSONDecode decoding arrays fails if single element decoding fails
- Swift Pass array by reference?
- Swift Replace String array at index
- Swift Set to Array
- Swift Display HTML data in a label or textView
- Swift Display HTML data in a label or textView
- Swift Sort array of objects alphabetically
- Swift Sort array of objects alphabetically

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.