Swift Closures-ready

Source: Internet
Author: User

I gave the closure in Swift a definition: closures are self-contained anonymous function code blocks that can be used as expressions, function arguments, and function return values, and the result of a closure expression is a type of function.

Closures in Swift are similar to blocks of code in OBJECTIVE-C,anonymous inner classes in Java.

Using a closure expression

The closure expressions in Swift are flexible and have the following standard syntax format:

{(parameter list ), return value type in

Statement Group

}

Where the argument list is the same as the parameter list in the function, the return value type is similar to the return value type in the function, but the difference is that the IN keyword is followed .

Swift offers a variety of closure simplification methods, which I'll cover in several different ways:

1. Simplification of type inference

Type inference is the strength of swift, andSwift can infer parameter types and return value types based on the context environment. The following code is a standard form of closure:

{(A:int, b:int), Intin

Return a + b

}

Swift can infer that the parameters A and B are of type int, and the return value is also of type int. The simplified form is as follows:

{(A, B) in return a + b}

{A, B in return a + b} //Parameter list brackets can also be omitted

2. Hide return keyword

In a closure, the statement group has only one statement, such as return a + B, which is the return statement. The preceding keyword return can be omitted and omitted in the following form:

{A, b in A + B}

The following example code is modified using this simplified method:

[HTML]View PlainCopyprint?
  1. Func Calculate (opr:string)-> (int,int)-> Int {
  2. var result: (int,int)-> Int
  3. Switch (OPR) {
  4. Case "+":
  5. result = {A, b in A + b}//return keyword omitted
  6. Default
  7. result = {A, b in-a}//return keyword omitted
  8. }
  9. return result
  10. }

The premise of omitting is that there is only one return statement in the closure.

3. Omitting parameter names

Swift provides the function of omitting parameter names, we can use $, $, $ ... To specify the parameters in the closure, "$" refers to the first argument,"the second argument," "$" refers to the third argument, and so on $n +1 refers to the nth parameter.

Using the parameter name ellipsis function, the argument list definition must be omitted from the closure, andSwift can infer the type of these abbreviated parameters. The argument list is omitted, and the inkeyword needs to be omitted. The name of the parameter is omitted as follows:

{$ + $}

The following example code is omitted after using the parameter name:

[HTML]View PlainCopyprint?
  1. Func Calculate (opr:string)-> (int,int)-> Int {
  2. var result: (int,int)-> Int
  3. Switch (OPR) {
  4. Case "+":
  5. result = {$ + +}//omitted with parameter name
  6. Default
  7. result = {$-$}//omitted with parameter name
  8. }
  9. return result
  10. }
  11. Let F1: (int,int)-> Int = Calculate ("+")
  12. Print ("Ten + 5 = \ (f1 (10,5))")
  13. Let F2: (int,int)-> Int = Calculate ("-")
  14. Print ("ten- 5 = \ (F2 (10,5))")

4. Use closure return value

The closure expression is essentially a function type and has a return value, and we can use the return value of the closure directly in the expression. To re -Modify the Add and sub closures, the sample code is as follows:

[HTML]View PlainCopyprint?
    1. Let c1:int = {(A:int, B:int)-> Int in
    2. Return a + b
    3. } (10,5)
    4. Print ("Ten + 5 = \ (c1)")

Explanation: assign a value to C1, followed by a closure expression. However, the closure expression cannot be assigned directly to C1 because C1 is of type int and the return value of the closure is required. This requires that a pair of parentheses (10,5) be followed by the curly braces at the end of the closure , with the parentheses (10,5) passing the parameters for the closure.

A closure expression can be passed as a function parameter, and if the closure expression is long, it affects the readability of the program. A trailing closure is a closure expression that is written after the function brackets, and the function supports calling it as the last argument.

Let's look at a sample code:

[HTML]View PlainCopyprint?
  1. Func Calculate (opr:string, Funn: (int, int)-> int) {
  2. The last parameter Funn is (int,int)-> Int function type, Funn can receive a closure expression
  3. Switch (OPR) {
  4. Case "+":
  5. Print ("Ten + 5 = \ (Funn (10,5))")
  6. Default
  7. Print ("ten- 5 = \ (Funn (10,5))")
  8. }
  9. }
  10. Calculate ("+", Funn: {(A:int, B:int)-> Int in return a + B})//Call
  11. Calculate ("+") {(A:int, B:int)-> Int in return a + B}//Call, this form is the trailing closure
  12. Calculate ("+") {$ + $}//call, this form is a trailing closure

It should be noted that the closure must be the last parameter of the parameter list if the calculate function is defined in the following form:

Func Calculate (funn: (int, int) ->int, opr:string) {

...

}

Because the closure expression is not the last parameter, the call to the Calculate function cannot be written using a trailing closure.

Swift Closures-ready

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.