Swift3.0-closure's @autoclosure and @escaping

Source: Internet
Author: User
Tags closure

(a) Autoclosure:

Autoclosure can automatically package a sentence into a closure. However, the notation with input parameters is not supported

        func Autoclosure (_ Check: @autoclosure (),Bool) {                if  check () {                        print (  "true")        }    }

You can write it directly at this time:

Autoclosure (2>3)

(b) @escaping:

In the previous version of the closure of the time without adding @escaping. The current version, if the closure does not have a callback parameter return value, is not required @escaping. But if the closure passes the parameters. There will be a hypothesis. That is, the contents of the block in the parameter are completed before the function is returned. This means that the call to block is synchronized.

Simply put, if the closure is called before the end of the function, it is noescape.

Closures are called after the function is completed, where the call exceeds the scope of the function and is the escape closure.

A callback that ends after a network request is escaped. The closure is executed after a period of time since the request was initiated.

All closures in the swift3.0 are default non-escaping and do not require @noescape. If it's escaping, @escaping says.

Delay operation, Network load, etc. all need @escaping.


Func clorse (Completion: @escaping (_ JSON: [String])()) {Let Workingqueue= dispatchqueue (Label:"Workingqueue") Workingqueue.async {//deferred OperationPrint"work hard") Thread.Sleep (fortimeinterval:2.0) Let JSON= ["AC","MYM"] DispatchQueue.main.async {print ("End Work")                                //main thread Update callbackcompletion (JSON)}} }

Swift3.0-closure's @autoclosure and @escaping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.