block异步返回值

block异步返回值

可以信号量semaphore 或 栅栏形式,都可以


+ (NSString *)selector
{
    dispatch_semaphore_t signal = dispatch_semaphore_create(0);
    
    __block NSString *str;
 
    // 模拟block异步
 dispatch_async(dispatch_get_global_queue(0, 0), ^{
        str = @"str";
        dispatch_semaphore_signal(signal);
    });

    dispatch_semaphore_wait(signal, DISPATCH_TIME_FOREVER);
    return str;

你可能感兴趣的:(block异步返回值)