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
- Capturing Values by Reference
- Escaping Closures
- Capture Lists and Weak Self
- Autoclosures