0Pricing
Swift Academy · Lesson

Escaping Closures

Store closures that outlive their function.

Escaping vs Non-Escaping

By default a closure parameter is non-escaping: it must be called before the function returns. Mark it @escaping if it may be stored and called later.

A Non-Escaping Closure

This closure runs synchronously and is gone when the function returns:

func run(_ work: () -> Void) {
    work()
}
run { print("done now") }  // "done now"

All lessons in this course

  1. Capturing Values by Reference
  2. Escaping Closures
  3. Capture Lists and Weak Self
  4. Autoclosures
← Back to Swift Academy