阻止 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) = // ...