In C/Obj-C, we do a typedef like this typedef int MYINT;
which is clear.
在C/ object -C中,我们使用这种类型定义;这是明确的。
Doing typedef for a block -typedef void (^MyBlock) (int a);
为一块做typedef typedef无效(^ MyBlock)(int);
Now, we can use MyBlock
.
现在,我们可以使用MyBlock。
Shouldn't it be like - typedef void (^MyBlock) (int a) MyBlock;
similar to #define
?
它不应该像typedef空白(^ MyBlock)(int)MyBlock;类似于#定义?
How the syntax works?
语法是如何运作的吗?
2 个解决方案
#1
61
See Declaring a Block Reference in "Blocks Programming Topics":
参见在“块编程主题”中声明块引用:
Block variables hold references to blocks. You declare them using syntax similar to that you use to declare a pointer to a function, except that you use ^ instead of *.
块变量保存对块的引用。您声明它们使用语法类似于用来声明一个指针指向一个函数,除了你使用^ *。
So
所以
typedef void (^myBlock) (int a);
defines a the type of a block using the same syntax as
使用与之相同的语法定义块的类型
typedef void (*myFunc) (int a);
declares a function pointer.
声明一个函数指针。
See e.g. Understanding typedefs for function pointers in C for more information about function pointers.
要了解更多关于函数指针的信息,请参见C中的函数指针的typedefs。
#2
18
Also from "Blocks Programming Topics", creating a type for blocks should be like this:
同样,从“块编程主题”中,为块创建类型应该如下所示:
typedef returnType (^blockName)(argument1, argument2, ...)
Below is a very simple practical example:
下面是一个非常简单的实际例子:
typedef float (^MyBlockType)(float, float);
MyBlockType AddTwoFloat = ^(float a, float b) {return a + b;};
MyBlockType MultiplyTwoFloat = ^(float a, float b) {return a * b;};
float c = AddTwoFloat(1, 2); //c = 3
float d = MultiplyTwoFloat(1, 2); //d = 2
#1
61
See Declaring a Block Reference in "Blocks Programming Topics":
参见在“块编程主题”中声明块引用:
Block variables hold references to blocks. You declare them using syntax similar to that you use to declare a pointer to a function, except that you use ^ instead of *.
块变量保存对块的引用。您声明它们使用语法类似于用来声明一个指针指向一个函数,除了你使用^ *。
So
所以
typedef void (^myBlock) (int a);
defines a the type of a block using the same syntax as
使用与之相同的语法定义块的类型
typedef void (*myFunc) (int a);
declares a function pointer.
声明一个函数指针。
See e.g. Understanding typedefs for function pointers in C for more information about function pointers.
要了解更多关于函数指针的信息,请参见C中的函数指针的typedefs。
#2
18
Also from "Blocks Programming Topics", creating a type for blocks should be like this:
同样,从“块编程主题”中,为块创建类型应该如下所示:
typedef returnType (^blockName)(argument1, argument2, ...)
Below is a very simple practical example:
下面是一个非常简单的实际例子:
typedef float (^MyBlockType)(float, float);
MyBlockType AddTwoFloat = ^(float a, float b) {return a + b;};
MyBlockType MultiplyTwoFloat = ^(float a, float b) {return a * b;};
float c = AddTwoFloat(1, 2); //c = 3
float d = MultiplyTwoFloat(1, 2); //d = 2