How to answer
A non-escaping closure is guaranteed to run (or be discarded) before the function it was passed to returns, so it can't outlive the call. An escaping closure can be stored or called later, after the function returns, and the compiler requires you to mark that parameter @escaping. Because an escaping closure can outlive the call, it keeps its captures alive, which is where retain cycles and [weak self] come in. If it may be called from another thread or actor, it should also be @Sendable.
Key points
- Closure parameters are non-escaping by default; the compiler can optimize them and you don't need
self.for implicit captures - Need
@escapingwhen the closure is stored in a property, called after an asynchronous operation, or passed to another escaping parameter - Optional closure parameters (
(() -> Void)?) are implicitly escaping - Escaping closures strongly capture
selfand other references; use[weak self]when the closure is stored by somethingselfowns - Escaping closures called from another thread or isolation domain should be
@Sendable(Swift 6 checks their captures) - Modern code often replaces completion handlers with
asyncfunctions, which removes most escaping closures
If they dig deeper
Capture lists ([weak self], [unowned self], capturing values like [id]). withoutActuallyEscaping for passing a non-escaping closure to an API that expects escaping. Why Task { } closures are sending rather than @Sendable. Wrapping a completion-handler API with withCheckedContinuation.
Common pitfalls
Forgetting @escaping when storing a closure (compile error). Retain cycles from storing a closure that captures self strongly in a property of self. Adding [weak self] to non-escaping closures like map (unnecessary). Calling a completion handler zero times or twice on some code path. Capturing mutable state in a @Sendable closure.
How to use it
// Non-escaping (default): runs before the function returns
func measure(_ work: () -> Void) -> Duration {
ContinuousClock().measure { work() }
}
// Escaping: called later, after the request completes, on a background queue
func loadImage(_ url: URL, completion: @escaping @Sendable (Data?) -> Void) {
URLSession.shared.dataTask(with: url) { data, _, _ in
completion(data)
}.resume()
}
@MainActor
final class AvatarController {
var avatar: Data?
private var onUpdate: (() -> Void)?
func observe(_ handler: @escaping () -> Void) { // stored, so must be @escaping
onUpdate = handler
}
func start(url: URL) {
observe { [weak self] in // avoid self -> closure -> self cycle
print(self?.avatar?.count ?? 0)
}
Task { [weak self] in // modern alternative: async + Task
let data = try? await URLSession.shared.data(from: url).0 // nil on failure, like loadImage
self?.avatar = data
}
}
}Best practices
- Use
@escapingonly when the closure genuinely outlives the call - Add
@Sendableto escaping callbacks that may run on another thread - Use
[weak self]in escaping closures thatselfstores (directly or indirectly) - Prefer
asyncfunctions over completion handlers in new APIs - Call a completion handler exactly once on every path
Related concepts
- Retain cycles, capture lists, weak vs unowned,
@Sendableclosures, async/await