如何将类型为dispatch_block_t的块传递给方法

问题描述 投票:0回答:1

在下面的代码中,我试图将类型为dispatch_block_t的块传递给dispatch_async,如下所示。我尝试了以下]]

        dispatch_async(defaultPriority, block1(2, 3000));
    dispatch_async(defaultPriority, (^block1)(2, 3000));
    dispatch_async(defaultPriority, ^(block1)(2, 3000));

但是,由于将块传递给dispatch_asynch方法的方式导致生成的错误,因此代码不可编译。请让我知道如何解决它。它。如果可以的话,您可以提供不同的方式来传递它,那就太好了。

main

    dispatch_block_t ^block1(int, int) = ^(int x1, int delay) {
        [NSThread sleepForTimeInterval: delay];
    };
    dispatch_queue_t defaultPriority = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_queue_t backgroundPriority = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

    dispatch_async(defaultPriority, block1(2, 3000));
 synchName andId: (NSString *) synchId;

@end

在下面的代码中,我试图将类型为dispatch_block_t的块传递给dispatch_async,如下所示。我尝试了以下dispatch_async(defaultPriority,block1(2,3000)); ...

objective-c grand-central-dispatch objective-c-blocks dispatch-queue
1个回答
0
投票

dispatch_block_t被定义为不带参数的闭包:

© www.soinside.com 2019 - 2024. All rights reserved.