Escaping closure
suggest changeFrom the Swift documentation:
@escaping
Apply this attribute to a parameter’s type in a method or function declaration to indicate that the parameter’s value can be stored for later execution. This means that the value is allowed to outlive the lifetime of the call. Function type parameters with the escaping type attribute require explicit use of self. for properties or methods.
class ClassThree {
var closure: (() -> ())?
func doSomething(completion: @escaping () -> ()) {
closure = completion
}
}
The completion closure is stored and can run after doSomething returns, so the compiler requires @escaping.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents