Swift Escape closure

Source: Internet
Author: User

definition
A closure is said to escape a function when the closure was passed as a argument to the function, but was called after the function returns. When your declare a function that takes a closure as one of their parameters, you can write @escaping before the parameter ' s Type to indicate this closure is allowed to escape.
The closure is described as: When the closure is passed as a parameter to a function, but the call is after the function return. When you declare a function that takes a closure as a parameter, you can write the @escaping keyword in the parameter type to indicate that the closure allows escape.
Application
One way that a closure can escape are by being stored in a variable this is defined the function. As a example, many functions that start a asynchronous operation take a closure argument as a completion. The function returns after it starts the operation, but the closure isn ' t called until the operation is completed-the Clos Ure needs to escape and to be called later. For example:
In one case the closure can escape, and the closure is stored in the variable defined outside the function. For example, many functions that turn on asynchronous operations take closures as completion handlers. This function has been return after opening this asynchronous operation. However, the closure does not occur until the asynchronous operation completes. To implement the function that the closure is called later, the closure needs to escape. For example:

The Somefunctionwithescapingclosure (_:) function takes a closure as its argument and adds it to a array that ' s declared O Utside the function. If you didn ' t mark the parameter's this function with @escaping, you would get a compile-time error.

Marking a closure with @escaping means your have to refer to self explicitly within the closure. For example, in the code below, the closure passed to Somefunctionwithescapingclosure (:) was an escaping closure, which MEA NS It needs to refer to self explicitly. In contrast, the closure passed to Somefunctionwithnonescapingclosure (:) is a nonescaping closure, which means it can Refe R to self implicitly.
This function makes a closure as one of his parameters and adds the closure to an array declared outside the function. If you do not mark this parameter as a escaping closure, you will get a compile error. The
create an escape closure means that you must display the reference self within the closure. Instead, Somefunctionwithnonescapingclosure (_:) is a non-escape closure, which means that it can implicitly reference itself.

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.