handler swift 4 code example
Example 1: swift how to add a completion block to a function
Simple Swift 4.0 example:
func method(arg: Bool, completion: (Bool) -> ()) {
print("First line of code executed")
completion(arg)
}
How to use it:
method(arg: true, completion: { (success) -> Void in
print("Second line of code executed")
if success {
print("true")
} else {
print("false")
}
})
Example 2: swift completion handler
func thisNeedsToFinishBeforeWeCanDoTheNextStep(completion: (String) -> ()) {
let stringToPassOutOfTheCompletionHandler = "The quick brown fox"
completion(stringToPassOutOfTheCompletionHandler)
}