Swift Closure _002_swift Closure Package

Source: Internet
Author: User

//: Playground-noun:a Place where people can playImport UIKit//-----closures-------////is a separate block of code that can be passed and used in code//closure in Swift is similar to block in OC//A function is a special kind of closure, a closed packet with a name.//1. Basic Use/*closure Syntax: {(parameter 1, parameter 2), return value type in closure code}*/func Add (A:int, B:int)-Int {returnA +b}let Closure= {(A:int, b:int), Intinch        returnA +B}add (1B:2) Closure (1,2)//the type of closure is the type of functionLet Closure2: (int, int)->int = {(A:int, b:int), intinch        returnA +B}//the simplification of the closure syntax, if you want to omit the type of parameters and return values in the closure, you need to explicitly specify the type of the closure variableLet closure3: (int, int)->int = {A, binch    returnA +B}//single-expression-style return: If there is only one expression in the code of the closure, return can be omittedLet Closure4: (int, int)->int = {A, binchA +B}closure4 (Ten,5)//parameter names can also be omittedLet Closure5: (int, int)->int = {$0+ $1}closure5 (1,2)//2. Using functions and closures to implement the traversal of elements in an array (execution of a piece of code for each element in a group)Func Emnumeratearray (array: [int], function: (int),Void) {         forIteminchArray {function (item)}}//the function that each element will executeFunc Printitem (varitem:int) {Item++print (item)}let array= [1,2,3,7]//Emnumeratearray (Array, Function:printitem)Let closure6= {(item:int)-Voidinchprint (item)}emnumeratearray (array, function:closure6) Emnumeratearray (array, function: {(item:int)-Voidinchprint (item)})//3. Trailing closures: If the closure is the last parameter, you can write the closure expression after the function parameter () to increase the readability of the CodeEmnumeratearray (Array) {(item:int), Voidinchprint (item)}//4. Closure Capture ValueFunc makeincrement (Forincrementor amount:int), (Void)Int {varTotal =0        //nested functions can capture variables or constants of external functions//func incrementor (), Int {//        //Total + = Amount//print ("total = \ (total)")//        //return Total//    }//    //return Incrementor//closures can capture externally defined variables or constantsLet Incrementor1: (void)->int = {(void)->intinch Total+=Amount Print ("Total = \ (total)")        returnTotal }returnIncrementor1}let closure7= Makeincrement (forincrementor:2) Closure7 () closure7 ()//closures are reference typesLet Closure8 = Makeincrement (forincrementor:3) Closure8 () Closure8 ( ) Let Closure9=closure8closure9 ()varA =0 //Intvarb =AA= A +1print (b)

Swift Closure _002_swift Closure Package

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.