Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you enumerate over an array of blocks?

self.myArray = @[ [^{ NSLog(@"a"); } copy],
                  [^{ NSLog(@"b"); } copy]];

... Later ....

[self.myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    // This syntax is wrong, but I hope you get my intention
    void (^) (void) block = obj;  
    block();
}];

How do you cast out the block when enumerating over it? (Bonus if you can do it without a typedef)

like image 948
Robert Avatar asked Apr 26 '13 13:04

Robert


2 Answers

[self.myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    void (^block)() = obj;  
    block();
}];

Or directly in the argument list:

[self.myArray enumerateObjectsUsingBlock:^(void (^block)(), NSUInteger idx, BOOL *stop) {
    block();
}];
like image 184
Mike Weller Avatar answered Nov 01 '22 07:11

Mike Weller


This should work:

void (^block )(void) = obj;
like image 33
Sergey Kalinichenko Avatar answered Nov 01 '22 07:11

Sergey Kalinichenko