Escaping vs non-escaping, autoclosures
Understand non-escaping (default) closures vs @escaping (stored or used after return), and use @autoclosure for nicer call sites with lazy evaluation.
Escaping vs non-escaping, autoclosures is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 3 lessons in total.
Overview
Goal: Know when a closure is non-escaping (default) vs @escaping (stored/async), and how @autoclosure makes call sites cleaner.
Non-escaping default
Closure parameters are non-escaping by default—invoked before the function returns and never stored.
// Non-escaping by default: used immediately inside the function.
func applyTwice(_ f: (Int) -> Int, to x: Int) -> Int {
return f(f(x)) // called synchronously; not stored
}
let doubled = applyTwice({ $0 * 2 }, to: 3)
print(doubled) // 12Escaping closure
Mark the parameter @escaping if you store the closure or execute it after return (e.g., timers, async work).
// Need @escaping when storing for later use:
var tasks: [() -> Void] = []
func addTask(_ work: @escaping () -> Void) {
tasks.append(work) // stored to run later
}
addTask { print("run later") }
tasks.forEach { $0() }Async completion
Async APIs typically take @escaping completion handlers, called after the function returns.
func fetchData(completion: @escaping (String) -> Void) {
// simulate async: call completion in the future
// (In real apps, use DispatchQueue or URLSession)
completion("done") // still escaping by contract (may be called later)
}
fetchData { result in print(result) }Autoclosure
@autoclosure lets callers pass an expression instead of writing a closure, improving readability.
// @autoclosure converts an expression into () -> Bool automatically.
func logIf(_ condition: @autoclosure () -> Bool) {
if condition() { print("Condition is true") }
}
let x = 3
logIf(x > 1) // no braces needed; expression is turned into a closureAutoclosure + escaping
@autoclosure @escaping stores the passed expression to evaluate later—be mindful of captured state and timing.
// You can combine @autoclosure with @escaping:
// (be careful: the expression is captured and evaluated later)
var checks: [() -> Bool] = []
func enqueueCheck(_ condition: @autoclosure @escaping () -> Bool) {
checks.append(condition) // stored to run later
}
var threshold = 10
enqueueCheck(threshold > 5) // captures current variable by reference semantics
threshold = 3
print(checks.map { $0() }) // re-evaluates with updated thresholdEscaping rule
Quick check: When is @escaping required?
Recap
Recap: Closures are non-escaping by default. Use @escaping if stored or called later. Use @autoclosure for cleaner call sites, and combine with @escaping carefully.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Escaping vs non-escaping, autoclosures” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Escaping vs non-escaping, autoclosures”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 3 lessons in total.
What will I learn in “Escaping vs non-escaping, autoclosures”?
Understand non-escaping (default) closures vs @escaping (stored or used after return), and use @autoclosure for nicer call sites with lazy evaluation. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.
How long does the “Escaping vs non-escaping, autoclosures” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Closures & Capture basics
- Capture lists — weak/unowned & value capture
- Escaping vs non-escaping, autoclosures