这是来自 apple blocks docs我很难理解这一点,请有人解释一下
If you don’t explicitly declare the return value of a block expression, it can be automatically inferred from the contents of the block. If the return type is inferred and the parameter list is void, then you can omit the (void) parameter list as well. If or when multiple return statements are present, they must exactly match (using casting if necessary).
最佳答案
1) 如果没有显式声明 block 表达式的返回值,则可以自动从 block 表达式的内容推断出返回值 区 block
区 block
void ^(float aFloat) {
printf("value is :%f",aFloat);
};
也可以写成
^(float aFloat) {
printf("value is :%f",aFloat);
};
没有说返回类型。返回类型是自动推断的。对于返回非空值的 block 来说也是如此。
2) 如果推断出返回类型并且参数列表为 void,则也可以省略 (void) 参数列表
这有点难以理解和令人困惑,因为文档明确指出
A block that takes no arguments must specify void in the argument list.
我相信这种 block 是可能的。
^{
printf("Hello world");
}
其中返回值是推断出来的,因此不需要 void。我对此不太确定,所以希望得到一些澄清。
3) 如果或当存在多个 return 语句时,它们必须完全匹配(必要时使用强制转换)。
简单来说,一个 block 必须只返回一种类型的数据。它不能返回 int 和 float。
关于ios - 难以理解 block 编程的这一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13047631/