-3

What Is an Escaping Closure? What is the difference between escaping and non-escaping closures in Swift 3?

Raja Jawahar
  • 7,314
  • 8
  • 45
  • 56
  • 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
  • 2
    https://cocoacasts.com/what-do-escaping-and-noescaping-mean-in-swift-3/ – Salman Ghumsani Sep 14 '17 at 08:11

1 Answers1

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 @escaping before 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

Hamish
  • 74,809
  • 18
  • 177
  • 265
Hiren
  • 270
  • 1
  • 14