理解复杂的块语法 [英] Understanding complex block syntax

查看:112
本文介绍了理解复杂的块语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Objective C和iOS开发的初学者,但是13年的.NET资深人士。我很难在心理上绘制以下声明,其来自使用Objective C进行编程指南:

I'm a beginner to Objective C and iOS development, but a 13-year .NET veteran. I'm having a hard time mentally diagramming the following declaration, which came from the Programming with Objective C guide:

void (^(^a)(void (^) (void))) (void) = ...

它被用作一个例子为什么我应该使用 typedef 来定义块,但我想了解我正在寻找的内容,以便更好地了解块定义语法。

It's used as an example of why one should use typedef to define blocks, but I want to understand what I'm looking at to better get a feel for block definition syntax in the first place.

以下是我到目前为止的图表:

Here's how I've diagrammed it so far:

我遇到问题的地方是我理解基本语法的方法:

Where I'm running into problems is that here is how I understand the basic syntax:

[return_val] (^[block_name]) ([block_args]) = ...

如果是这样的话在这种情况下,我所拥有的是一个返回void且没有参数的块,但命名为(^ a)(void(^)void)。这意味着我的块的名称,而不是一个直的字符串,本身就是一个块。

If that's the case, then what I have is a block that returns void and has no arguments, but is named (^a) (void (^) void). Meaning the name of my block, rather than being a straight string, is itself a block.

显然我在这里遗漏了一些东西。有人可以对此有所了解吗?根据该网站,它简化为:

Clearly I'm missing something here. Can someone please shed some light on it? According to the site, it simplifies to this:

typedef void (^SimpleBlock) (void);
SimpleBlock (^complexBlock) (SimpleBlock) = ...

我只是缺少如何。

编辑:第三个空格应该在括号中。我修好了。它在图像中是错误的,但我不想为此重做整个图像。 :)如果结果是问题的根源,我会在这里解决。

推荐答案

In你的例子你错过了第三个​​的一些括号 void

In your example you're missing some parentheses for the third void

void (^(^a)(void (^)(void)))(void)

现在让我们分解吧。从函数返回块的基本语法是:

Now let's break it down. The basic syntax to return a block from a function is:

void (^f())(void) { 
    return ^{}; 
}

在此示例中,返回的块不带参数并返回void

In this example, the returned block takes no arguments and returns void.

现在让我们构建你的例子。

Now let's build your example.

void     (^myBlock)(void);                       // Block returning void, taking no args
void     (^myBlock)(void (^)(void));             // Block returning void, taking block as arg
int      (^myBlock)(void (^)(void));             // Block returning int, taking block as arg
void (^  (^myBlock)(void (^)(void))  )(void);    // Block returning block, taking block as arg

我已将每行的中心部分对齐到让它更容易阅读。所以困难的部分似乎正在回归一个街区。在最后一行中,我们使用前面描述的语法从函数返回一个块。

I've aligned the central part in each line to make it easier to read. So the difficult part seems to be returning a block. In the last line we used the syntax I described earlier to return a block from a function.

显然 typedefs 让它更容易阅读。

编辑:

考虑这个例子,在第一行,我替换 int 对于,使用直观返回语法:


Consider this example where, in the first line, i replace int for a block with the intuitive return syntax:

void (^ )(void) (^myBlock)(void (^)(void));          // Syntax we 'intuitively would use'
void (^         (^myBlock)(void (^)(void))  )(void); // Official syntax

我不是100%肯定我要说的话,但是我怀疑这种奇怪语法的原因是编译器中的解析器不会混淆。第一个直观语法会让编译器认为我们有一个块没有参数返回void ,剩下的字符将被视为语法错误。

I'm not 100% sure of what I'm about to say, but my suspicion is that the reason for this weird syntax is so that the parser in the compiler doesn't get confused. The first 'intuitive' syntax would make the compiler think that we have a block taking no arguments returning void, and the remaining characters would be considered syntax error.

在我看来,语法是你没有太多质疑的东西(当然你可以批评它),因为它是语言设计的一部分,我们必须遵循规则(由一些希望智能设置)工程师)为我们的代码编译。

In my opinion, syntax is something you don't question too much (you can criticize it, of course) because it's part of the design on a language, and we have to follow the rules (set by some hopefully smart engineers) for our code to compile.

这篇关于理解复杂的块语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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