How to Use Trailing Closures in Swift
Use trailing closures in Swift when a function’s last parameter is a closure, especially for UI actions, collection transforms, animations, and async callbacks. This is the cleanest and most idiomatic Swift style.
What you’ll build or solve
You’ll learn how to use trailing closures in Swift with single closures, multiple closures, and shorthand syntax. You’ll also know when regular argument syntax is clearer.
Learn Swift on Mimo
When this approach works best
This approach is the right choice when the closure logic is closely tied to the function call.
Common real-world scenarios include:
mapandfilter- SwiftUI buttons
- Animations
- Completion handlers
- Network callbacks
This is a bad idea when the closure is tiny and regular parentheses improve scanability in dense chained code.
Prerequisites
You only need:
- Basic Swift closures
- Functions
- Familiarity with parameters
Step-by-step instructions
Step 1: Use a trailing closure with array methods
If the closure is the last argument, move it outside the parentheses.
Swift
let scores = [95, 88, 76]
let doubled = scores.map { score in
score * 2
}
This is much cleaner than inline closure parentheses.
Step 2: Use trailing closures in SwiftUI
SwiftUI relies heavily on this syntax.
Swift
Button("Save") {
print("Saved")
}
The button action closure reads naturally.
This is one of the most common Swift patterns.
Step 3: Use shorthand syntax for short closures
Small trailing closures become very compact.
Swift
let sorted = scores.sorted { $0 > $1 }
This keeps transforms highly readable.
Step 4: Use multiple trailing closures
Swift supports labeled trailing closures.
Swift
performRequest {
print("Success")
} onFailure: {
print("Failed")
}
This is excellent for async APIs.
What to look for:
- Use when the last argument is a closure
- Great for SwiftUI and array transforms
- Shorthand works well for one-liners
- Multiple trailing closures improve async readability
- Prefer normal syntax in dense chains when clearer
Examples you can copy
Map
Swift
items.map { $0.id }
SwiftUI action
Swift
Button("Continue") {
Async callback
Swift
loadData {
Common mistakes and how to fix them
Mistake 1: Using parentheses plus trailing closure
What the reader might do:
Swift
scores.map() { $0 * 2 }
Why it breaks: the empty parentheses are unnecessary.
Corrected approach:
Remove them.
Mistake 2: Overusing shorthand in complex blocks
What the reader might do:
Use $0 and $1 across many lines.
Why it breaks: readability drops.
Corrected approach:
Use named parameters.
Mistake 3: Forcing trailing syntax when it hurts readability
What the reader might do:
Use trailing closures in dense nested chains.
Why it breaks: scanability suffers.
Corrected approach:
Use regular closure arguments when clearer.
Troubleshooting
If the syntax looks awkward, verify the closure is the last parameter.
If multiple callbacks exist, use labeled trailing closures.
If shorthand is unclear, switch to named parameters.
If chains become dense, move logic into named helpers.
Quick recap
- Use trailing closures when the last argument is a closure
- Great for SwiftUI and collection transforms
- Shorthand is great for short logic
- Use labeled multiple closures for async APIs
- Prefer clarity over style rules
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