如何将一个块作为参数传递给Objective C中的另一个块 [英] How to pass a block as an argument into another block in Objective C

查看:72
本文介绍了如何将一个块作为参数传递给Objective C中的另一个块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试定义一个以块为参数的块.

I'm trying to define a block that takes a block as an argument.

下面的代码行有什么问题?

What's wrong with the following line of code?

id (^cacheResult)(NSString *, id(^)(void)) = ^(NSString *name, id(^)(void)block) {
    NSObject *item = nil;
    block();
    return item;
};

为什么编译器会不断给出Parameter name omittedExpected ")"之类的错误?

Why does the compiler keep giving errors like Parameter name omitted and Expected ")"?

推荐答案

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 ^

这篇关于如何将一个块作为参数传递给Objective C中的另一个块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆