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.

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

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