2019/12/13 Swift クロージャー パート3(capturing)

Capturing Values(キャプチャ)

sample1-1

func createCounter(countBy countBy:Int)->()->Int{
    var count:Int=0
    return {()-> Int in
        count += countBy
        return count
    }
}

var counter1=createCounter(countBy: 8)
print(counter1())  //8
print(counter1())  //16
print(counter1())  //24

var counter2=createCounter(countBy: 10)
print(counter2())  //10
print(counter2())  //20
print(counter2())  //30
print(counter2())  //40

var