Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6897203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:08:22+00:00 2026-05-27T07:08:22+00:00

I have a callback method that I got to work, but I want to

  • 0

I have a callback method that I got to work, but I want to know how to pass values to it.

What I have is this:

@interface DataAccessor : NSObject
{
    void (^_completionHandler)(Account *someParameter);

}


- (void) signInAccount:(void(^)(Account *))handler;

The code above works, but I want to pass values to the method. How would this look? Something like:

- (void) signInAccount:(void(^)(Account *))handler user:(NSString *) userName pass:(NSString *) passWord;

?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:08:23+00:00Added an answer on May 27, 2026 at 7:08 am

    I’m not entirely sure what you’re trying to do there – your callback is a block… is that intentional? I would expect your method to look something like this:

    - (void)signInAccountWithUserName:(NSString *)userName password:(NSString *)password;
    

    If the intention of your callback is to execute some additional code (specified when you call the method) on completion, then a block would be useful. For example, your method would look like this:

    - (void)signInAccountWithUserName:(NSString *)userName
                             password:(NSString *)password
                           completion:(void (^)(void))completionBlock
    {
        // ...
        // Log into the account with `userName` and `password`...
        //
    
        if (successful) {
            completionBlock();
        }
    }
    

    And then call the method like so:

    [self signInAccountWithUserName:@"Bob"
                           password:@"BobsPassword"
                         completion:^{
                             [self displayBalance];  // For example...
                         }];
    

    This method call would log the user into the account and then as soon as that is complete, show the balance. This is clearly a contrived example, but hopefully you get the idea.

    If this is not the kind of thing you intended, then simply use a method signature like the one above.


    EDIT (A better example using the successful variable):

    A better design would be to pass a Boolean back in the completion block that describes how well the login went:

    - (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);
    }
    

    You’ll also see that this time around we’re checking that the completionBlock parameter is not nil before calling it – this is important if you want to allow the method to be used without a completion block. You might use this method like so:

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

    Better still (if you can excuse the swaths of examples!), if it would be useful for the user to know the reason for the failure, return an NSError object:

    - (void)signInAccountWithUserName:(NSString *)userName
                             password:(NSString *)password
                           completion:(void (^)(NSError *error))completionBlock
    {
        // Attempt to log into the account with `userName` and `password`...
    
        if (loginSuccessful) {
            // Login went ok. Call the completion block with no error object.
            if (completionBlock != nil) completionBlock(nil);
        } else {
            // Create an error object. (N.B. `userInfo` can contain lots of handy 
            // things! Check out the NSError Class Reference for details...)
            NSInteger errorCode;
            if (passwordIncorrect) {
                errorCode = kPasswordIncorrectErrorCode;
            } else {
                errorCode = kUnknownErrorCode;
            }
            NSError *error = [NSError errorWithDomain:MyLoginErrorDomain code:errorCode userInfo:nil];
            if (completionBlock != nil) completionBlock(error);
        }
    }
    

    The caller can then make use of the NSError in the completion block to decide how to proceed (most likely, to describe to the user what went wrong). This kind of pattern is slightly less common (though perfectly valid); mostly NSErrors are returned by pointer indirection, for example in the NSFileWrappers -initWithURL:options:error: method:

    NSError *error;
    NSFileWrapper *fw = [[NSFileWrapper alloc] initWithURL:url options:0 error:&error];
    // After the above method has been called, `error` is either `nil` (if all went well),
    // or non-`nil` (if something went wrong).
    

    In the login example, however, we are probably expecting the login attempt to take some amount of time to complete (for example logging into an online account), so it is perfectly reasonable to make use of a completion handler that passes an error back.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a vertical testimonials belt and i have this method that animates its
Eg. I have following delegate method I want to use as a callback function
Apple have conveniently created a callback method that allows you to check that the
I have got an API that I have to work with. The API is
I have a method that is firing off threads to do some work. There
I have a set of callback classes that I use for handling callbacks with
I have a list of callback functions that I need to invoke when an
I have a function function callback(obj){...} Is it okay to pass in more objects
I have a webservice that when called without specifying a callback will return a
I currently have a controller method that returns a JsonResult: public JsonResult EditField(int FieldID,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.