Objective-C Language Block Typedefs


Example

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

If you declare a block type as a typedef, you can then use the new type name instead of the full description of the arguments and return values. This defines Operation as a block that takes two doubles and returns a double.

The type can be used for the parameter of a method:

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

or as a variable type:

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

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

Without the typedef, this is much messier:

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

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