An escaping closure is a closure that can outlive the function it was passed to. It means the closure can be called after the function returns, often stored or used asynchronously.
Click to reveal answer
beginner
Why do we need to mark a closure with @escaping in Swift?
We mark a closure with @escaping when the closure is stored or called after the function returns. This tells Swift to keep the closure alive beyond the function's lifetime.
Click to reveal answer
intermediate
What happens if you try to use a non-escaping closure as escaping in Swift?
Swift will give a compile-time error because non-escaping closures are expected to be used only during the function execution, not after it returns.
Click to reveal answer
beginner
How do you declare a function parameter as an escaping closure in Swift?
You add the @escaping keyword before the closure type in the function parameter list, for example: func doSomething(completion: @escaping () -> Void) {}
Click to reveal answer
intermediate
Give a simple example of an escaping closure usage in Swift.
Example: func fetchData(completion: @escaping (String) -> Void) { DispatchQueue.main.asyncAfter(deadline: .now() + 1) { completion("Data loaded") } } This closure runs after the function returns.
Click to reveal answer
What does the @escaping keyword indicate in Swift closures?
AThe closure cannot be stored
BThe closure is optional
CThe closure runs immediately and only once
DThe closure can be called after the function returns
✗ Incorrect
The @escaping keyword means the closure might be called after the function it was passed to has returned.
Which of these closures must be marked with @escaping?
AA closure stored in a property for later use
BA closure called immediately inside the function
CA closure passed as a parameter but never called
DA closure that returns a value immediately
✗ Incorrect
Only closures that outlive the function, like those stored for later use, must be marked @escaping.
What error occurs if you forget @escaping for a closure that escapes?
AWarning only
BCompile-time error
CRuntime crash
DNo error, code runs fine
✗ Incorrect
Swift enforces @escaping at compile time to prevent misuse of closures.
Can a non-escaping closure be stored for later use?
ANo, it must be used during the function execution
BYes, always
COnly if marked optional
DOnly if it returns Void
✗ Incorrect
Non-escaping closures cannot be stored or used after the function returns.
Which keyword is used to mark a closure parameter that might escape?
A@inline
B@autoclosure
C@escaping
D@discardableResult
✗ Incorrect
The @escaping keyword marks closures that can be called after the function returns.
Explain what an escaping closure is and why Swift requires the @escaping keyword.
Think about when the closure runs compared to the function.
You got /3 concepts.
Describe how to declare and use an escaping closure in a Swift function with a simple example.
Consider a closure called after a delay or stored in a property.
You got /3 concepts.
Practice
(1/5)
1. What does the @escaping keyword indicate in Swift closures?
easy
A. The closure can be called after the function returns
B. The closure is executed immediately within the function
C. The closure cannot capture variables from the surrounding scope
D. The closure is only used for synchronous tasks
Solution
Step 1: Understand closure execution timing
Closures without @escaping run inside the function before it returns.
Step 2: Role of @escaping
Marking a closure @escaping means it can be stored and called later, after the function ends.
Final Answer:
The closure can be called after the function returns -> Option A
Quick Check:
@escaping means delayed closure call [OK]
Hint: Escaping closures run later, not immediately [OK]
The closure is called asynchronously after the function returns, so it must be marked @escaping.
Step 2: Check function parameter declaration
The parameter lacks @escaping, causing a compile error.
Final Answer:
Missing @escaping before closure parameter -> Option A
Quick Check:
Async closure needs @escaping [OK]
Hint: Async closure parameters must be @escaping [OK]
Common Mistakes:
Forgetting @escaping for async closures
Assuming closure is synchronous by default
Adding unnecessary optional to closure
5. You want to store a closure to call later after a network request finishes. Which is the correct way to declare and use this escaping closure in Swift?