Block使用小笔记
匿名函数的写法着实让人难记
As a local variable:
1
2
3
returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
void (^payFinishBlock)(BOOL finish) = ^(BOOL finish) {};
As a property:
1
2
3
@property (nonatomic, copy, nullability) returnType (^blockName)(parameterTypes);
@property (nonatomic, copy) void (^successBlock)();
As a method parameter:
1
2
3
4
5
- (void)someMethodThatTakesABlock:(returnType (^nullability)(parameterTypes))blockName;
handler:(void (^)(UIAlertView *alertView, NSInteger buttonIndex))block
handler:(void (^)(void))block
As an argument to a method call:
1
[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];
As a typedef:
1
2
3
typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};
本文由作者按照 CC BY 4.0 进行授权