OngaLog

最近GOが楽しくて、

Swift callBackのサンプル

callBackのサンプルを作ってみる。簡単に実装できる。にしてもIBMのSwift Sandboxはいいな使いやすい。

swiftlang.ng.bluemix.net

class Dog {
    func run(param:Int, callBackClosure:(Bool, Int) -> Void) -> Void{
        var SuccessOrError = false
        SuccessOrError = true
        let basicSpeed=20
        let speed:Int=basicSpeed*param;
        callBackClosure(SuccessOrError, speed)
    }
}

let callbackClosure1 = { (success:Bool, speed:Int) -> Void in
    if(success){
        print("Dog Name Shiba run " + String(speed) + "km per hour" )
    }else{
        print("Error")
    }
}

let callbackClosure2 = { (success:Bool, speed:Int) -> Void in
    if(success){
        print("Dog Name Tiwawa run " + String(speed) + "km per hour" )
    }else{
        print("Error")
    }
}

let _dog = Dog()
_dog.run(param:2, callBackClosure: callbackClosure1)
_dog.run(param:1, callBackClosure: callbackClosure2)