What Is an Escaping Closure? What is the difference between escaping and non-escaping closures in Swift 3?
Asked
Active
Viewed 3,401 times
-3
-
There are lots of Q&A's about that topic already. Please clarify which part in the [documentation](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-ID546) is unclear to you. – Martin R Sep 14 '17 at 07:54
-
2https://cocoacasts.com/what-do-escaping-and-noescaping-mean-in-swift-3/ – Salman Ghumsani Sep 14 '17 at 08:11
1 Answers
3
Escaping Closures
A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns. When you declare a function that takes a closure as one of its parameters, you can write
@escapingbefore the parameter’s type to indicate that the closure is allowed to escape.[...]
var completionHandlers: [() -> Void] = [] func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) { completionHandlers.append(completionHandler) }
Refer Apple Doc for better understanding : https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html