Suppose I have function that does some stuff
func doSomethingAwesome(completion:(success:Bool) -> Bool) {
//some stuff
}
How can I transform it to trailing closure ? How can I transform control from body of function to completion block ?
Suppose I have function that does some stuff
func doSomethingAwesome(completion:(success:Bool) -> Bool) {
//some stuff
}
How can I transform it to trailing closure ? How can I transform control from body of function to completion block ?
It is already trailing closure. You can call completion in body:
func doSomethingAwesome(completion:(success:Bool) -> Bool) {
//some stuff
let result = completion(success: true)
}
And thats how you can use trailing closure syntax calling this func:
doSomethingAwesome {
success in
return success
}
You can just call that function (which already has a trailing closure) by doing:
doSomethingAwesome{ finished in
if finished{
return true
}
return false
}
Your completion handler is of type Bool. So Bool is used as I showed here.