0Pricing
Swift Academy · Lesson

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 — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)  // 12

Escaping 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 closure

Autoclosure + 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 threshold

Escaping 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.

Frequently asked questions

Is the “Escaping vs non-escaping, autoclosures” lesson free?

Yes — the full text of “Escaping vs non-escaping, autoclosures” is free to read here on the web, and the Swift Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

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

  1. Closures & Capture basics
  2. Capture lists — weak/unowned & value capture
  3. Escaping vs non-escaping, autoclosures
← Back to Swift Academy