Call a block from another function

I need help with blocks

I want to do something like this:

- (void)function {
      self.block =  ^(NSArray * array){
        NSLog(@"BLOCK %@", array);
    };
}


- (void)anotherFunction {
   block(array)
}

Is it possible?!)

+4
source share
2 answers

Yes, this is normal ... you can do the following

@property (nonatomic, copy) void (^block)(NSArray *array);

...

- (void)function {
    self.block =  ^(NSArray * array){
        NSLog(@"BLOCK %@", array);
    };
}


- (void)anotherFunction {
    if (self.block)
        self.block(array);
}
+2
source

Yes maybe

the first: -

type def in your .h file, the type of block you are going to use helps.

typedef void (^blockForYou)(NSArray*);//typedef helps in recognizing and better    understanding blocks and easy to use as well

now create a block type property, copy it.

@property(nonatomic,copy)blockForYou yourBlock;

Now in the block copy the .m file to your property.

self.yourBlock=^(NSArray* arrayData){
        //get your array here
    };

now from another function just call your block.

self.yourBlock(<pass your array here>);

hope this helps.

+1
source

All Articles