阻止 Typedef

typedef double (^Operation)(double first, double second);

如果将块类型声明为 typedef,则可以使用新类型名称而不是参数的完整描述并返回值。这将 Operation 定义为一个块,它接受两个双精度并返回一个 double。

该类型可用于方法的参数:

- (double)doWithOperation:(Operation)operation 
                    first:(double)first 
                   second:(double)second;

或作为变量类型:

Operation addition = ^double(double first, double second){
    return first + second;
};

// Returns 3.0
[self doWithOperation:addition
                first:1.0
               second:2.0];

如果没有 typedef,这会更加混乱:

- (double)doWithOperation:(double (^)(double, double))operation
                    first:(double)first
                   second:(double)second;

double (^addition)(double, double) = // ...