IRD
IRD

Reputation: 1157

How to call a method that include a block as a parameter and NSString as the block parameter

I have a method like this

-(void)GetPostPreperation :(NSMutableURLRequest *)request :(BOOL)isGet :(NSMutableDictionary *)jsonBody :(void(^)(NSString*)) compblock

how can I pass parameter into this within a block? I tried like this. but it giving me an syntax error, Expected Expression

This is the way I tried

 [self GetPostPreperation:request :isGet :jsonBody :(^NSString*)str
 {
     return str;

 }];

This is how I defined my block

typedef void(^myCompletion)(NSString *);

I want to assign a NSString value to the block parameter within my GetPostPreperation method and check and return it from the block calling method.How can I do this? Please help me. Thanks

Upvotes: 0

Views: 79

Answers (4)

Pravin Tate
Pravin Tate

Reputation: 1130

Declare variable name in block so you can access it while using that block (string)

// typedef of block    
    typedef void(^myCompletion)(NSString * string);



        NSMutableURLRequest * req = [NSMutableURLRequest new];
            NSMutableDictionary * dict = [NSMutableDictionary new];

       /// method calling     
            [self GetPostPreperation:req
                               isGet:true
                            jsonBody:dict
                               block:^(NSString *string) {
                                   if ([string isEqualToString:@"xyz"]) {

                                   }
                                   else
                                   {

                                   }
                               }];

When ever your method have more than one argument then it will always write as like follow (i.e. isGet:(BOOL)isGet) so it easy while calling that method

// method with block

    -(void)GetPostPreperation :(NSMutableURLRequest *)request isGet:(BOOL)isGet jsonBody:(NSMutableDictionary *)jsonBody block:(myCompletion)string
    {
        string(@"yes");

    }

Upvotes: 0

Leo
Leo

Reputation: 24714

Use it in this way,here str is an input not return

[self GetPostPreperation:request :true : jsonBody :^(NSString * str) {
        //here use
        NSLog(@"%@",str);

    }];

Update

do not return anything from this block.The block type is void(^)(NSString*),the return is void

Upvotes: 1

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39978

[self GetPostPreperation:nil :YES :nil :^(NSString * string) {
  //your code
  NSLog(@"%@", string);
}];

I would also suggest you to change the method definition as below:

-(void)GetPostPreperation :(NSMutableURLRequest *)request
                          :(BOOL)isGet :(NSMutableDictionary *)jsonBody
                          :(void(^)(NSString* string)) compblock 

This will give you auto suggestion when typing this method and you can just hit enter to create it.

enter image description here

Upvotes: 2

Mrugesh Tank
Mrugesh Tank

Reputation: 3560

just make block with String parameter and pass that block to method.

like below

void (/*Block name*/^failure)(/*block formal parameter*/NSError *) = ^(/*block actual parameter*/NSError *error) {
        NSLog(@"Error is:%@",error);
    };

[self myMethod:failure];

Upvotes: 1

Related Questions