ios – 有一种方法可以像这样添加块到NSOperationQueue

前端之家收集整理的这篇文章主要介绍了ios – 有一种方法可以像这样添加块到NSOperationQueue前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在努力了解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对象指针的其他原因(例如,建立操作之间的依赖关系等).

原文链接:https://www.f2er.com/iOS/329858.html

猜你在找的iOS相关文章