Learning to learn swift from scratch (day 22)--closures that thing!

Source: Internet
Author: User

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

I give Swift Closures in 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.

Swift is similar to the closure in objective-c in the code block, Java the anonymous inner class in the.  

Using a closure expression

Swift The closure expression in is flexible and has 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 inch key word.

Swift There are a number of different types of closures that I'll cover in the following ways:  

1 , type inference simplification

type inference is Swift 's strengths, Swift parameter types and return value types can be inferred from the context environment. The following code is a standard form of closure:

{(A:int, b:int), Intin return a + b}


Swift can infer the parameters a and the b is a Int type, the return value is also Int type. 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 Key Words

Within the closure statement group there is only one statement, such as return a + b and so on, then this statement is a 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 {var result: (int,int), int switch (OPR) {case "+"  : result = {A, b in a + b}//return keyword omitted default:result = {A, b in-a}//return keyword omitted} return Result


the premise of omission is that only one of the closures return statement.  

3 , omit Parameter name

Swiftparameter names are providedomittedfunction, we can use $, $, ...to specify the parameters in the closure, $refers to the first parameter, $refers to the second argument, $refers to the third argument, and so on$n +1refer to sectionNa parameter.

using parameter names omitted function, the parameter list definition must be omitted from the closure, Swift The types of these abbreviation parameters can be inferred. 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 {      var result :  (Int,int)-> int         switch  (OPR)  {    case  "+"  :         result = {$0 + $1}//uses the parameter name to omit     default:         result = {$0 - $1}//using the name of the parameter omitted      }    RETURN RESULT} LET F1: (int,int)-> Int=  Calculate ("+") Print ("10 + 5 =\ (F1 (10,5))")  let f2: (int,int)-> int=  Calculate ("-") Print ("10 - 5 = \ (F2 (10,5))") 

4 , using the 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:

Let C1:int = {(A:int,b:int), Int in return a + B} (10,5) print ("Ten + 5 =\ (C1)")


Explanation: Give C1 assignment, followed by a closure expression. However, the closure expression cannot be assigned directly to C1because 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
650) this.width=650; "title=" 00.png "alt=" Wkiol1bvyjwqgf0uaaas2mbeznc951.png "src=" http://s3.51cto.com/wyfs02/M02/ 7c/aa/wkiol1bvyjwqgf0uaaas2mbeznc951.png "/>
More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php


This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1746406

Learning to learn swift from scratch (day 22)--closures that thing!

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.