Closures-1 closure expressions

Source: Internet
Author: User

/*

Closure definition: Closures are self-contained function code blocks that can be passed and used in code.

Closures can capture and store references to any constants and variables in the context in which they are located. This is called closed merge wrapped with these constants and variables, commonly known as closures. SWIFT2.0 manages all memory operations involved in the capture process

Closures take one of the following three forms:

* Global function: A closure that has a name but does not capture any value

* nested functions; a closure that has a name that captures the value in its enclosing function field

* Closure expression: is an anonymous closure written using lightweight syntax that captures variables or constant values in context.

Closure expressions have a concise style, and Swift2.0 encourages developers to make grammatical optimizations in common scenarios, with the main optimizations as follows:

* Use context to infer parameters and return value types

* Implicit return expression closure, that is, single-expression closure can omit the return keyword

* Abbreviation of parameter name

* Use of hanging tail closure package

*/

Closed-Packet expression

The/*1sorted function (requires passing in two parameters:

* Arrays of known types

* Closure function, the closure function needs to pass in the same two values as the array type, and returns a Boolean type value to tell the sorted function whether the first parameter passed in after the sort end is preceded or followed by the second argument. If the first parameter value appears before the second parameter value, the sort closure function needs to return true and vice versa to return false.

This example sorts an array of type string, so the sort closure function type must be (string, string)-Bool.

)*/

One way to provide a closure function is to write a normal function that conforms to its type requirements and pass it as the second parameter of the sort function;

Let city = ["Beijing", "Shanghai", "Guangzhou", "Shenzhen", "Nanjing"]

Func backwards (s1:string,s2:string)->bool{

return S1 < S2

}

var newcity = City.sort (backwards)

/*

Let city = [1,3,4,5,2,3,5,7,1,4,6]

Func backwards (S1:int,s2:int)->bool{

return S1 < S2

}

var newcity = City.sort (backwards)

*/

/*2 using a closure expression, the closure expression syntax has the following general form:

No function name {

(parameters), return type in

Code snippet

}*/

var newCity2 = City.sort ({

(s1:string,s2:string)->bool in

return s1 > S2

})

Print ("\ (newCity2)")

/* Obviously, here we use a closure expression, in the "sort" method, directly through the closure expression to the definition of "func backward" in the previous segment of the Code

This function definition is removed, the definition of a function is concise, and the function body part of the closure is introduced by the keyword in. The keyword indicates that the closure's parameters and return value type definitions have been completed, and the function body of the closure is about to begin.

/*3 parameter inference based on context

Because the sort closure function is passed in as a parameter to the sorted function, swift2.0 can infer the type of its parameter and return value. Sorted expects the second parameter type to be a function of (string,string)->bool, so string,string and bool types are not necessarily required as part of the closure expression definition. Because all types can be inferred correctly, the return arrows and the parentheses around the parameters can also be omitted

Such as:

*/

var newCity3 = City.sort ({s1,s2 in

return s1 > S2

})

Print ("\ (newCity3)")

/*4 single-expression closure eliminates the return keyword, and if the closed statement has only a single-line expression, you can omit the return */

var NewCity4 = City.sort ({

S1,s2 in

S1 > S2

})

Print ("\ (NewCity4)")

/* In this example, the second argument function type of the "sorted" function is clear that the closure must return a Bool type value. Because the closure function body contains only a single expression "S1 > s2", the expression returns the Bool type value, so there is no ambiguity here, the return keyword can be omitted. */

/*5 parameter name abbreviation, Swift2.0 automatically for the parameter name abbreviation function for the inline function, you can call the closure parameter directly through "$0,$1,$2" (0 is the first parameter). If you use a parameter name abbreviation in a closure expression, you can omit the definition of it in the closure parameter list, and the type that corresponds to the abbreviation for the parameter name is inferred from the function type. The "in" keyword can also */

var newCity5 = City.sort ({

$ > $

})

Print ("\ (newCity5)")

/* operator function: The string type of the swift2.0 defines the implementation of strings for the greater than sign ">", which takes two arguments of type string as a function and returns a value of type bool. This coincides with the type of function required by the second parameter of the sorted function, so you can simply pass a greater than sign, and Swift can automatically infer that you want to use a string function that is greater than the number, such as */

var newCity6 = City.sort (>)

Print ("\ (NEWCITY6)")

Closures-1 closure expressions

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.