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

... 之后 ....

[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();
}];

枚举时如何抛出块?(如果你可以在没有 typedef 的情况下做到这一点,那就是奖励)

4

3 回答 3

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

或者直接在参数列表中:

[self.myArray enumerateObjectsUsingBlock:^(void (^block)(), NSUInteger idx, BOOL *stop) {
    block();
}];
于 2013-04-26T13:24:25.183 回答
1

这应该有效:

void (^block )(void) = obj;
于 2013-04-26T13:24:25.017 回答
0

从 art-divin 的评论来看,这可能是一种更好的方法。

NSOperation* aOp = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"a");}];
NSOperation* bOp = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"b");}];

NSOperationQueue* opQueue = [[NSOperationQueue alloc] init];
[opQueue setSuspended:YES];
[opQueue addOperation:aOp];
[opQueue addOperation:bOp];

... 之后

[opQueue setSuspended:NO];
于 2013-04-28T14:13:33.043 回答