Learn about closures in Swift.

Source: Internet
Author: User
Tags closure closure definition

Closures in Swift are equivalent to blocks in OC, and its purpose is to wrap a piece of code that can be invoked when necessary.

 Closure definition :

{(Type list), return value in

More than one swift statement

Execute Code Insert Coding

}

The actual closure code is created as follows:

1 inch 2     // code block 3     if value1>value2{4         return  value15     } 6     return value2 7 8 }

Note: 1. Here we define a square variable, the type of which can be deduced by the return value type of the closure, and the type of square (int,int)->int type is derived from the code.

2. The meaning of this closure code is to compare the size of two numbers, return the largest number and assign a value to square

3. Note the name of the parameter in the closure, if you do not define an external parameter name, when we call there is no hint (Xcode 7.2, maybe the version will be improved after, after all, Xcode in writing Swift many tips are not given), if we give two parameters to provide an external name, The system will give you a hint when we call this closure.

Call the closure code as follows:

  

3 5 ) print (max)

A complete closure expression is required to define the formal parameter type and return a value type, but since the parameter type can be inferred from the context in swift, when the variable determines the type, the new parameter type and the return value type in the closure can be omitted:

1 inch  (Value1 > value2?) value1:value2) 2 }

Note: 1. This code beginner may look more laborious, may not understand (Int,int)->int is what, it is actually a type, is the type of closure, this code is in fact the closure of the type is all written to the variable type definition. Beginners in the practice can be written according to the norms, and so skilled when you can see this code, now just as understanding.

2. In addition, you can see that the return value here omits return , which can be omitted when the code execution body has only one line of code.

3. You can also omit the parameter name, and the in key value can be omitted if the closure expression omits the formal parameter name. In this case, the closure expression is in the form of $ ... name to use first, second, third parameter.

  Closures can capture constants and variables in the context

Closures can access or modify variables or constants in the context in which they are located (constants can only be accessed and cannot be modified), a process known as capture. Even if the scope of the defined variables or constants does not already exist, the closures can still access or modify them.

 The procedure is as follows

1 //defines a function that returns a value of type: ()->[string]2Func Makearray (ele:string),[String]3 {4     //defines an array that contains no elements5var arr:[string] = []6     //defines a nested method that returns a string array7Func addelement ()[String]8     {9         //Add an ele element to the ARR arrayTen arr.append (ele) One         returnarr A     } -      -     returnaddelement the      -}

Note: It can be seen from the above program that no parameters and variables are defined in arrelement (), but can still be accessed to arr and ele because it captures the two variables or parameters in its context by the context, so arrelement () Can be taken without a defined condition.

Learn about closures in Swift.

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.