3

The objective c code seems like this:

- (void)signInAccountWithUserName:(NSString *)userName
                         password:(NSString *)password
                       completion:(void (^)(BOOL success))completionBlock
{
    // Log into the account with `userName` and `password`...
    // BOOL loginSuccessful = [LoginManager contrivedLoginMethod];

    // Notice that we are passing a BOOL back to the completion block.
    if (completionBlock != nil) completionBlock(loginSuccessful);
}

and this method usage is:

[self signInAccountWithUserName:@"Bob"
                       password:@"BobsPassword"
                     completion:^(BOOL success) {
                         if (success) {
                             [self displayBalance];
                         } else {
                             // Could not log in. Display alert to user.
                         }
                     }];

How can I implement it in Swift? What is the equivalent implementation?

2 Answers 2

4
func signInAccount(username:NSString!, password:NSString!, completionBlock:((Bool)->())?) {
    if completionBlock {
        completionBlock!(true)
    }
}

signInAccount("Bob", "BobPassword") {
    (var success) in
    println("\(success)")
};

signInAccount("Bob", "BobPassword", nil)
Sign up to request clarification or add additional context in comments.

Comments

0

This is how you would implement methods with callbacks in swift.

func signInAccountWithUsername(userName:String!, password: String!, completion: (Bool) -> Void){

  completion(false)
}



signInAccountWithUsername("Swift", "secret swift", {
    success in

    if success{
      println("Success")
    }else{
      println("Failure")
    }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.