Wednesday, September 14, 2016

closure in swift

Closure is nuts!

Try run this code, and you will know what I am talking about


typealias IntegerFunction = Int -> ()

func  gamCounterSample() -> Int -> Void {
    func increment(i: Int){
        print("Integer passed in: \(i)")
    }
    return increment
}


func  gameCounter() -> IntegerFunction {
    var counter = 0
    
    func increment(i : Int){
        counter += i
        print("Counter value \(counter)")
    }
    
    return increment
}

let aCounter = gameCounter()
aCounter
aCounter(1)
aCounter(1)

No comments:

Post a Comment