I was wondering how to make a completion handler for a function I'm creating in Swift 3. This is how I did my function right before I updated to Swift 3:
func Logout(completionHandler: (success: Bool) -> ()) {
backendless.userService.logout(
{ ( user : AnyObject!) -> () in
print("User logged out.")
completionHandler(success: true)
},
error: { ( fault : Fault!) -> () in
print("Server reported an error: \(fault)")
completionHandler(success: false)
})}
But now I can't figure out the best approach that works right now.
In Swift 3 the function parameter labels in closures are gone.
Remove all occurrences of success:
and add @escaping
func Logout(completionHandler:@escaping (Bool) -> ()) {
backendless?.userService.logout(
{ user in
print("User logged out.")
completionHandler(true)
},
error: { fault in
print("Server reported an error: \(fault)")
completionHandler(false)
})
}
And use it
Logout() { success in
print(success)
}
No need to include Parameter names in Swift 3. auto completion suggest to add @esacping
func Logout(completionHandler:@escaping (Bool) -> ()) {
backendless?.userService.logout(
{( user: Any?) -> (Void) in
print("User logged out.")
completionHandler(true)
},
error: { ( fault : Fault?) -> (Void) in
print("Server reported an error: \(fault)")
completionHandler(false)
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.