我正在努力了解NSOperationQueue,并尝试创建最简单的例子.我有以下几点:
NSOperationQueue *myOQ=[[NSOperationQueue alloc] init]; [myOQ addOperationWithBlock:^(void){ NSLog(@"here is something for jt 2"); }]; [myOQ addOperationWithBlock:^(void){ NSLog(@"oh is this going to work 2"); }];
但是想这样做:
void (^jt)() = ^void(){ NSLog(@"here is something for jt"); }; void (^cl)() = ^void(){ NSLog(@"oh is this going to work"); }; NSOperationQueue *myOQ=[[NSOperationQueue alloc] init]; [myOQ addOperation:jt]; [myOQ addOperation:cl];
这可能是后者吗?可以将块转换为NSOperation吗?
提前
解决方法
你可以:
NSBlockOperation *jtOperation = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"here is something for jt"); }]; NSBlockOperation *clOperation = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"oh is this going to work"); }]; [myOQ addOperation:jtOperation]; [myOQ addOperation:clOperation];
话虽如此,我通常会添加操作,除非我真的需要NSOperation对象指针的其他原因(例如,建立操作之间的依赖关系等).