Swift2.1 Keyword @noescape Introduction

Source: Internet
Author: User

@noescape optimized compiler compilation, introduced by Swift 2.1.

function if there is a parameter is closure, you can add a keyword @noescape before the function return closure the end of the life cycle, you can optimize the compilation. (Closure is a reference type that can be referenced elsewhere, as follows:)

class a{var  B: (void  void )  Func Lalala (A: (void  ->,  void )) {self   b =  a//reference to Property B }} 

as in the case above Lalala parameter A is not marked @noescape

class a{var  B: (void  void )  Func AA (@noescape A: (void  ->,  void )) {self   b =  a//will report the error, the type is not the same }} 

class  a  {var b: void , void ) ! Func aanoescape  void ->void ) ) {A () //No error, this will not be called again externally}} 

@noescape also has a feature that marks the @noescape closure can eliminate the write reference self. Swift's normal closure is required when using self. This can be an exception. Because he is equivalent to a synchronous call and does not produce a circular reference, so do not deliberately emphasize self

Class a{varB: (Void  -Void)!    varK= 0//Used to invoke theFunc AA (@noescape A: (Void -Void) {A ()} func bb () {aa {()} -Void inchK= 1//General closure are to SELF.K = 1, @noescape do not need}    }}

Discussion: When will the @noescape be used? Now a lot of functional programming, such as having a sort, requires a comparison of the closure as a parameter, this closure will be the synchronous call complete to get the return value. This can put a @noescape in front, can optimize memory, reference self does not have to write self. Other closure that are externally referenced by the wait will not be @noescape, because it will escape. Generally speaking, this @noescape is not a good egg, but some people will use it, it may scare you. For example, I saw a use of this yesterday, this basic can be ignored. Apple Documentation Links

Swift2.1 Keyword @noescape Introduction

Related Article

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.