Back to Home

Completion Handlers in Swift

Shane Qi • 2016-02-19 16:13

Based on Xcode 7.2.1(7C1002)

If we want to do something only after a function is completed, we use completion handlers.

Declare:

func foo(name :String, completion: ((result:String?) -> Void)) {
    // Do something. 
    completion (result: "Hello, \(name)!") 
}

Use:

foo("Shane") {
    result in print("Got back: \(result)") 
}