GaoLi's Blog

如何在 Objective-C 中声明 Block

作为局部变量

1
returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};

作为属性

1
@property (nonatomic, copy, nullability) returnType (^blockName)(parameterTypes);

作为方法形参

1
- (void)someMethodThatTakesABlock:(returnType (^nullability)(parameterTypes))blockName;

作为方法调用时的实参

1
[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];

使用 typedef 定义别名

1
2
typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};