Welcome to Swift (Apple's official Swift document translation and annotations 38)---266~271 page (sixth--closures)

Source: Internet
Author: User

Trailing Closures (tail closure package)

If you need to pass a closure expression as a parameter to a function, and the position of the parameter is just the last of the function, and you have a very long closure expression, it is very effective to use the tail closure package (useful).

A closed packet is a closure expression written outside (or behind) the following function ():

Func somefunctionthattakesaclosure (Closure: ()) {

function body goes here

}

Here's how to call the This function without using a trailing closure:

Somefunctionthattakesaclosure ({

Closure ' s body goes here

})

Here's how to call the This function with a trailing closure instead:

Somefunctionthattakesaclosure () {

Trailing closure ' s body goes here

}

Note the point:

If a closure expression is the only parameter to a function, and you are using the tail closure, you can use the function name instead of a pair of parentheses () when calling the function.

With the string sort closures mentioned earlier, you can use the tail closure package as follows:

Reversed = sort (names) {$ > $}

When a closure expression is so long that a line cannot be finished, the closure is very useful. For example, the array type in Swift has a map method that receives a closure expression as a parameter that is called once for each element in the array. Returns a map value that has been modified for this element (possibly other types), as specified by the closure.

After a closure has been applied to each element in an array, the Map method returns a new array that contains the new mapping values in the same sequence as the original array.

The following example shows you how to convert an int array to a string array using the Map method in the form of a tail closure. Array [16,58,510] is used to create a new array ["Onesix", "Fiveeight", "Fiveonezero"]:

Let digitnames = [

0: "Zero", 1: "One", 2: "One", 3: "Three", 4: "Four",

5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"

]

Let numbers = [16, 58, 510]

The code above creates a mapping dictionary from the Digital to the English version, which also defines an integer array, ready to be converted to a string.

You can now use the numbers array to create a string array by passing a closure expression as the tail closure of the array map method. Note that when calling Numbers.map, you do not need to write parentheses () behind the map because the map method has only one parameter , and this came in just fine is the tail closure package:

Let strings = Numbers.map {

(var number), String in

var output = ""

While number > 0 {

Output = digitnames[number% 10]! + Output

Number/= 10

}

Return output

}

Strings is inferred to be of type string[]

Its value is ["Onesix", "Fiveeight", "Fiveonezero"]

The map function calls the closure expression to process the elements in each array. You do not need to specify the input parameter type of the closure, because the specific type is automatically determined based on the value of the element in the array.

In this example, the number parameter of the closure is defined as a variable parameter, so the value of the parameter can be modified within the closure without re-declaring a new local variable, passing the value of number to the local variable, and the closure expression specifying the return value type string.

Each time the closure expression is called, a string called output is created, which calculates the number on the last digit (by number%10 this operation), and then uses this number to find the corresponding string in the dictionary digitnames.

Note the point:

The digitnames of the calling dictionary is followed by an exclamation point!, because this method returns an optional value that indicates that the lookup failed if the key in the dictionary does not exist. In the above example, it ensures that number%10 is always legal, and exclamation marks! A string that is stored in an optional value is forced to be unpacked.

The string received from the dictionary digitnames is added to the output, which effectively establishes an inverted string (expression number%10, which gives 16 inside of 6, 58 inside of 8,510 0)

Then the variable number is divided by 10, because it is an integral type, so the result is an integral type, so 16 becomes 1, 58 becomes 5,510 and becomes 51.

This process repeats until the number/=10 value equals 0, and the output is returned by the closure, and the map function adds it to the output array.

In the example above, the use of the tail closure syntax is very concise, encapsulating the closure function, and does not need to unpack the entire closure within the map function.

Welcome to Swift (Apple's official Swift document translation and annotations 38)---266~271 page (sixth-closure)

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.