Swif (vi) Swift closures

Source: Internet
Author: User
Tags closure

////Main.swift//Lessonswiftsix////Created by Keyan on 15/9/13.//Copyright (c) 2015 Keyan. All rights reserved.//Import Foundation/*(1) Closure concept and classification self-contained function code block global function (famous) closure expression (anonymous)--can capture the context of a German constant or variable nesting function (*)*//*(2) The syntax of the closure expression, func funcName (parameter), return value type {EXECUTE statement} {(parameter)--return value type in execute statement}*/Let SayHello={println ("Hello World")}sayhello () Let add: (Int,int)->int ={(a:int,b:int)->intinch    returnA +b}println (Add (3,5))/*(3) Closure expression callback usage*/func Showarray (array: [Int]) { forXinchArray {print ("\ (x),")} println ()}func Bubblesort (CMP: (int,int)-int,inout array: [INT]) {Let CNT=Array.count for vari =1;i<cnt; i++ {         for varj =0; J < cnt-i;j++ {            if(CMP (array[j],array[j+1]) == -1) {Let T=Array[j] Array[j]= array[j+1] Array[j+1] =T} }}}varArray = [ -,3,2, -,8]//Showarray (Array)//Let intcmp = {//(A:int,b:int), Int in//if a>b{//return-1//        //    }//else If a < b {//return 1//    }//Else//    {//return 0//    }//}//Swift-specific type inferenceShowarray (Array)/*(4) Closure-expression optimization*///Bubblesort (&array,{////(A, B) in//Let x = $10,y = $//if x>y{//return-1//        //    }//else if x < y {//return 1//    }//Else//    {//return 0//    }//})Showarray (Array)/*(5) trailing closures*/Bubblesort ({//(A, b) inLet X = $0%Ten, y = $1%Ten    ifX>y{return-1            }    Else ifX <y {return 1    }    Else    {        return 0    }       }    , &Array)//Sort (&array,{////(A:int,b:int), Bool in////Return a< b//Return< $//    //})//trailing closuresSort (&Array) {    //(A:int,b:int), Bool in//return a < b    return$0< $1}showarray (Array)/*nested Functions*///func swapvalue (inout a:int,inout b:int)//{//Let t = a//A = b//B = t//}func bubblesortfunc (inout array: [Int]) {Let CNT=Array.count//put into internal nested functions acting on the insidefunc swapvalue (inout a:int,inout b:int) {Let T=a A=b b=T} for vari =1;i<cnt; i++ {         for varj =0; J < cnt-i;j++ {            if(array[j]>array[j+1]){//Let t = array[j]//Array[j] = array[j+1]//array[j+1] = tSwapvalue (&array[j], &array[j+1])            }        }    }}varArray1 = [5, +,Ten,2,3]bubblesortfunc (&array1) Showarray (array1)/*(7) Closed packet Capture value*///func Getincfunc (inc:int)-(Int)-int{//func Incfunc (v:int), Int {//return 10+v//    }//return Incfunc//}Func Getincfunc (Inc:int), (INT)int{varMT =Tenfunc incfunc (v:int)-Int {MT++returnINC + MT +v}returnIncfunc}let incFunc1= Getincfunc (3) Let IncFunc2= Getincfunc (3) println (INCFUNC1 (Ten)) println (IncFunc1 (Ten)) println (INCFUNC2 (Ten))

Swif (vi) Swift closures

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.