Eric Chuang
Eric Chuang

Reputation: 1017

How can I return the correct value in this block in iOS

I am getting a compiler error

incompatible block pointer types sending 'BOOL'

All I am trying to do is return the value in my completion block in the following code:

-(BOOL)registerUserWithUser:(NSString*)name withPhone: (NSString*)number withPassword: (NSString*)password{
GlobalFunctions *function = [GlobalFunctions sharedInstance];
[function registerUserWithName:name phone:number password:password completion:^(BOOL isSuccess, NSError *error) {
    return isSuccess;
}];    
}

I've read other similar answers but none really solves my issue directly. Is the value returned already before the block is complete?

Upvotes: 0

Views: 86

Answers (3)

Rashmi Ranjan mallick
Rashmi Ranjan mallick

Reputation: 6620

I think you want to pass the block as a method parameter. You can declare your method like this.

-(void)registerUserWithUser:(NSString*)name withPhone: (NSString*)number withPassword: (NSString*)password successHandler:(void (^)(BOOL isSuccess))myBlock;

Implementation will be somewhat like this. At the end, you can call your success handler block parameter.

-(void)registerUserWithUser:(NSString*)name withPhone: (NSString*)number withPassword: (NSString*)password successHandler:(void (^)(BOOL isSuccess))myBlock{

    /***
     ** Do your logic here **
     ** Then call the success handler block.
     ****/
    BOOL isSuccess= FALSE;
    myBlock(isSuccess);

}

You can call your method like this:

[self registerUserWithUser:@"xyz" withPhone:@"1234" withPassword:@"abcd" successHandler:^(BOOL success){
        NSLog(@"success: %d", success);
    }];

Hope it helps!!

Upvotes: 0

Leo
Leo

Reputation: 24714

Your method is async,so you cannot get return in traditional way. If you return something form block,it is the return for block, not for function.

I think it is better to pass your logic code in callback.

typedef void(^Callback)(BOOL success);

-(void)registerUserWithUser:(NSString*)name withPhone: (NSString*)number withPassword: (NSString*)password callBack:(Callback)callBack{
GlobalFunctions *function = [GlobalFunctions sharedInstance];
[function registerUserWithName:name phone:number password:password completion:^(BOOL isSuccess, NSError *error) {
    callBack(isSuccess);
    }];
 }

Then you can use it like this

  [self registerUserWithUser:@"" withPhone:@"" withPassword:@"" callBack:^(BOOL success) {
    if (success) {

    }else{

    }
}];

Upvotes: 0

James Webster
James Webster

Reputation: 32066

Is the value returned already before the block is complete?

In short: Yes! Blocks run asynchronously. The return from the block cannot be used as the return for the method.

However, that's not what's causing the compile time error. Your block doesn't have a return type:

^returnType(parameters)

but you are returning something inside the block anyway. Have a look here for more information on block syntax.

Upvotes: 2

Related Questions