Swift 2.0 Study Notes (Day 22)--Closures those things!

Source: Internet
Author: User

Original articles, welcome reprint. Reprint Please specify: Dongsheng's blog

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, and swift 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), Int in

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:

Func Calculate (opr:string), (Int,int)Int {varResult: (Int,int)IntSwitch(OPR) { Case "+": Result= {A, binchA + b}//The return keyword is omitted    default: Result= {A, binchA-B}//The return keyword is omitted    }    returnresult}

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, the number of the first argument, the second argument, 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, and Swift can infer the type of these abbreviated parameters. The argument list is omitted, and the in keyword needs to be omitted. The name of the parameter is omitted as follows:

{$ + $}

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

Func Calculate (opr:string), (Int,int)Int {varResult: (Int,int)IntSwitch(OPR) { Case "+": Result= {$0+ $1}//omitted by parameter name    default: Result= {$0- $1}//omitted by parameter name    }    returnresult} Let F1: (int,int)-Int = Calculate ("+") Print ("+ 5 = \ (f1 (10,5))") Let F2: (int,int)-Int = Calculate ("-") Print ("10-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:

inch                return A + b               } (5)   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.

Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
?
More Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie Classroom: http://www.zhijieketang.com
Luxgen Classroom Forum Website: http://51work6.com/forum.php

Swift 2.0 Study Notes (Day 22)--Closures those things!

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.