How does typedef-ing a block works

前端 未结 2 1154
忘掉有多难
忘掉有多难 2021-01-31 07:48

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);

No

2条回答
  •  执笔经年
    2021-01-31 08:16

    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
    

提交回复
热议问题