In C/Obj-C, we do a typedef like this typedef int MYINT;
which is clear.
Doing typedef for a block -typedef void (^MyBlock) (int a);
Now, we can use MyBlock
.
Shouldn't it be like - typedef void (^MyBlock) (int a) MyBlock;
similar to #define
?
How the syntax works?
A typedef, or a function-type alias, helps to define pointers to executable code within memory. Simply put, a typedef can be used as a pointer that references a function.
typedef is a predefined keyword. You can replace the name of the existing data type with the name which you have provided. This keyword helps in creating a user-defined name for an existing data type. You can also use the typedef keyword with structures, pointers, arrays etc.
The typedef in C/C++ is a keyword used to assign alternative names to the existing datatypes. It is mostly used with user-defined datatypes when the naming of the predefined datatypes becomes slightly complicated to use in programs.
typedef struct { int scruples; int drams; int grains; } WEIGHT; The structure WEIGHT can then be used in the following declarations: WEIGHT chicken, cow, horse, whale; In the following example, the type of yds is "pointer to function with no parameter specified, returning int ".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With