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就能获得奖励)
答案 0 :(得分:10)
[self.myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
void (^block)() = obj;
block();
}];
或直接在参数列表中:
[self.myArray enumerateObjectsUsingBlock:^(void (^block)(), NSUInteger idx, BOOL *stop) {
block();
}];
答案 1 :(得分:1)
这应该有效:
void (^block )(void) = obj;
答案 2 :(得分: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];