据我了解,可以使用 __block 修饰符使对象在块(和队列?)之间可用。
-(void)performRequest: (void (^)(NSArray* outcome))completion
{
dispatch_async(dispatch_get_global_queue(0,0), ^{
// do some request
__block NSArray * result = [[NSArray alloc]init]; //Outcome of the request.
completion(result);
});
}
让我们假设完成块将在主线程上 dispatch_async
我想应该可以使用没有 __block 标识符的值类型来做到这一点。我说得对吗?
-(void)performRequest: (void (^)(int outcome))completion
{
dispatch_async(dispatch_get_global_queue(0,0), ^{
// do some request
int result = 10; //Outcome of the request.
completion(result);
});
}