How to pass a block as an argument into another block in Objective C

浪子不回头ぞ 提交于 2019-12-04 02:29:52
id (^cacheResult)(NSString *, id(^)(void)) = ^(NSString *name, id(^block)(void)) {
    NSObject *item = nil;
    block();
    return item;
};

Blocks have similar syntax to function pointers. You have to declare block name after the ^

This is why typedef was invented. Embedding function pointers or block types like this is a pain. Try this instead:

typedef id (^ InnerBlock)(void);
typedef id (^ OuterBlock)(NSString *, InnerBlock);

It'll make working with block types a lot easier to read. :)

Did you possibly mean id(^block)(void) on the RHS of the assignment?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!