How to Use Map in Swift
Use map in Swift when you need to transform every value in a collection into a new collection of the same size. It is perfect for formatting text, converting models, extracting IDs, and building SwiftUI-friendly view data.
What you’ll build or solve
You’ll learn how to use map in Swift with arrays, dictionaries, optionals, and model transforms. You’ll also know when compactMap is the better fit.
Learn Swift on Mimo
When this approach works best
This approach is the right choice when every input value should produce exactly one output value.
Common real-world scenarios include:
- Extracting IDs
- Formatting prices
- Mapping API models
- Building display labels
- Transforming optional values
This is a bad idea when some values should be removed entirely.
Prerequisites
You only need:
- Basic Swift arrays
- Familiarity with closures
- Understanding of return values
Step-by-step instructions
Step 1: Transform simple values
The most common use is array transformation.
Swift
let scores = [95, 88, 76]
let doubled = scores.map { score in
score * 2
}
This returns:
Swift
[190, 176, 152]
The output keeps the same number of items.
Step 2: Extract model properties
A very common real-world use is pulling one field from structs.
Swift
struct User {
let id: Int
let name: String
}
let users = [
User(id: 1, name: "Alex"),
User(id: 2, name: "Sam")
]
let names = users.map { $0.name }
This is excellent for SwiftUI lists.
Step 3: Use map with optionals
Optionals also support map.
Swift
let score: Int? = 95
let label = score.map { "Score: \($0)" }
This keeps the result optional.
Step 4: Transform dictionaries
Dictionaries can map values too.
Swift
let settings = ["darkMode": true, "beta": false]
let labels = settings.map { key, value in
"\(key): \(value)"
}
This returns an array of transformed values.
What to look for:
maptransforms every element- Output size stays the same
- Great for model-to-view data
- Optionals support
map - Use
compactMapto remove nils
Examples you can copy
Double numbers
Swift
numbers.map { $0 * 2 }
Extract names
Swift
users.map { $0.name }
Optional label
Swift
score.map { "Value: \($0)" }
Common mistakes and how to fix them
Mistake 1: Using map when nils should disappear
What the reader might do:
Return optionals from the closure.
Why it breaks: the output becomes [Type?].
Corrected approach:
Use compactMap.
Mistake 2: Using map for side effects
What the reader might do:
Print values inside map.
Why it breaks: forEach is clearer.
Corrected approach:
Use forEach.
Mistake 3: Forgetting the returned collection
What the reader might do:
Call map without storing the result.
Why it breaks: the transformed values are discarded.
Corrected approach:
Assign the output.
Troubleshooting
If nils remain, switch to compactMap.
If the result is unused, assign it.
If the transform is side-effect-only, use forEach.
If SwiftUI lists need IDs, map directly to display models.
Quick recap
- Use
mapto transform every value - Output size stays the same
- Great for display-model transforms
- Optionals support
map - Use
compactMapto remove nils
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot