33

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.

Noah G.
  • 375
  • 1
  • 3
  • 14
  • https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html ? – Larme Sep 22 '16 at 15:41

2 Answers2

68

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)
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Do you happen to know why the variables changed in Swift 3? (Also, thank you very much! I'm going to try it when I get back to my computer.) – Noah G. Sep 22 '16 at 17:06
  • You have to take care of the order. – vadian Sep 22 '16 at 17:09
  • @vadian Why is it necessary to place @escaping before the parameter list in this closure? The closure does not seem to escape, it is called within the `func Logout` therefore the @escaping flag should not be specified in Swift 3. – bibscy Dec 27 '16 at 22:52
  • 1
    @bibscy All closures used as an asynchronous callback are `escaping`. – vadian Feb 26 '17 at 10:45
  • How do I access the Bool value once getting rid of parameter name success: ? – Tamil May 01 '17 at 07:26
  • @Tamil The value is returned in the closure of the calling method. I updated the answer. – vadian May 01 '17 at 08:07
  • @vadian i have a method signature getStatus(handler: &(Bool) -> Void)) i am able to access the bool but i am getting the error cannot pass immutable value of type '(Bool) ->()' as inout argument. Why is this error coming?. What does & in the signature mean? – Tamil May 01 '17 at 08:48
  • @Tamil Please ask a new question and add more information. – vadian May 01 '17 at 08:50
0

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)
    })
}
MSPL Apps
  • 49
  • 8