Pick the fix
Storing completion in a property fails: escaping closure captures non-escaping parameter. Which fix is right?
final class Loader {
private var onDone: (() -> Void)?
func start(completion: () -> Void) {
onDone = completion
}
}Hint 1 of 1
Does the closure outlive the function call?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Mark the parameter @escaping
Correct. It is stored and called later, so it escapes.
Why not the others?
- B Use
withoutActuallyEscapingand store it. That is only safe if the closure doesn't outlive the call; storing it breaks that promise. - C Mark it
@autoclosure. Autoclosure wraps an expression; it doesn't change escaping. - D Mark it
@Sendable. Sendable is about thread safety, not lifetime.