Overview - Escaping closures (@escaping)
What is it?
In Swift, an escaping closure is a closure that can outlive the function it was passed into. This means the closure might be called after the function returns. To mark a closure as escaping, you use the @escaping keyword. This tells Swift to keep the closure alive beyond the function's lifetime.
Why it matters
Without escaping closures, you couldn't safely store or delay work that needs to happen later, like network responses or animations. If Swift didn't distinguish escaping closures, it could cause memory leaks or crashes by freeing resources too early. Escaping closures let you write asynchronous and delayed code safely and clearly.
Where it fits
Before learning escaping closures, you should understand basic closures and function parameters in Swift. After this, you can learn about memory management, retain cycles, and asynchronous programming patterns like completion handlers and async/await.