Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between @noescape, @escaping and @autoclosure?

Tags:

ios

swift

I am converting code to Swift using Xcode 8, but compiler warns to add @escape in some of the nested functions already created in Swift 2.3, with closure syntax. I have found some other keywords also @noescape and @autoclosure, but I have some question regarding this :

  1. What is functional need of this keyword?
  2. What is impact of writing @escaping ?
  3. Is it necessary to write ?
  4. Is @autoclosure behave same as @escape?
  5. When to use which keyword and why ?

Here is Swift-evolution document, but not getting much from it.

like image 363
technerd Avatar asked Sep 21 '16 09:09

technerd


People also ask

What is the difference between @escaping and @nonescaping closures in Swift?

“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.” In order to be an escaping closure, the function to which the closure is passed must return before the closure is executed.

What does @escaping mean Swift?

Wrapping Up. In Swift, closures are non-escaping by default. This means that the closure can't outlive the function it was passed into as a parameter. If you need to hold onto that closure after the function it was passed into returns, you'll need to mark the closure with the keyword @escaping.

What is Autoclosure?

An autoclosure is a closure that's automatically created to wrap an expression that's being passed as an argument to a function. It doesn't take any arguments, and when it's called, it returns the value of the expression that's wrapped inside of it.

Why we use closures in Swift?

Using a closure to send back data rather than returning a value from the function means Siri doesn't need to wait for the function to complete, so it can keep its user interface interactive – it won't freeze up.


2 Answers

The most important difference is between @escaping and @noescaping (there is no such keyword in Swift 3!). When a closure is marked as @noescape, you can be sure that the closure won't be retained by the method (e.g. to perform an asynchronous call), therefore you don't have to worry about ownership cycles (there are some other minor benefits).

@escaping closures can be saved or called sometimes in the future therefore you have to be sure to handle ownership (e.g. [weak self]) correctly.

For @autoclosure see How to use Swift @autoclosure . In short, it allows you to skip braces around the closure in some situations.

The default (when not specified) is @noescaping in Swift 3 (see rationale). They keyword does not actually exist anymore. There is only @escaping.

like image 55
Sulthan Avatar answered Sep 29 '22 10:09

Sulthan


@escape means that your function can be called after method ends. @noescape means that your closure will be called before the end of function it passed to. @autoclosure means that you can omit writing closure braces, but it automatically becomes @noescape. @autoclosure creates an automatic closure around the expression. So when the caller writes an expression like 2 > 1, it's automatically wrapped into a closure to become {2 > 1} before it is passed to some function.

2 - Easier for compiler to perform optimisations.

3 - beter to write, to give compiler(or anybody that using your function) information how it behaves with your closure

4 - no

5 - described on the top of the answer

like image 38
Volodymyr Avatar answered Sep 29 '22 12:09

Volodymyr