Objective-C语言块类型定义
示例
typedef double (^Operation)(double first, double second);
如果将块类型声明为typedef,则可以使用新的类型名称,而不是参数和返回值的完整描述。这定义Operation为需要两次加倍并返回一次加倍的块。
该类型可用于方法的参数:
- (double)doWithOperation:(Operation)operation first:(double)first second:(double)second;
或作为变量类型:
Operation addition = ^double(double first, double second){ return first + second; }; //返回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) = //...