How can I retrieve a return value from a completion block?

北城余情 提交于 2019-11-26 22:11:39

You're missing some basics about asynchronous development with blocks. You can't have a dispatched block return from anywhere but its own scope. Think of each block as its own method, instead of inline code.

I think what you're looking for is something similar to this...

- (void)testWithHandler:(void(^)(int result))handler
{
    [obj somemethodwithcompeltionblock:^{
            int someInt = 10;
            dispatch_async(dispatch_get_main_queue(), ^{
                handler(10);
            });
      }
      ];
}


- (void)callSite
{
    [self testWithHandler:^(int testResult){
        NSLog(@"Result was %d", testResult);
    }];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!