Search code examples
iosobjective-creferenceblock

How I can send a parameter by reference in a block in obj-c


Do you know how I can send a parameter by reference in a block?

My function is similar to this:

I tried with this code:

//The function 
-(void)downloadObjects:(NSMutableSet**)set handler:(void(^block)(void))handler{
...
code
...
}

and this call

-(void)myFunction{
   __block NSMutablesSet *objects = [NSMutableSet new];
   [self downloadObjects:&objects handler:^(void(^block)(void)){
      [self show:objects];
   }];
}

And I receive a EXC_BAD_ACCESS error because my "objects" variable was deallocated before the use

I know that I can receive the objects by block response, but in my real case I want to receive it in this mode.


Solution

  • There's no need to pass your NSMutableSet to a block like this, you are only going to have memory management issues for no reason. You only need it this way if you want to return some value like an NSError.