How Do I Declare A Block in Objective-C?
As alocal variable:
```
returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
```
As aproperty:
```
@property (nonatomic, copy, nullability) returnType (^blockName)(parameterTypes);
```
As amethod parameter:
```
- (void)someMethodThatTakesABlock:(returnType (^nullability)(parameterTypes))blockName;
```
As anargument to a method call:
```
[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];
```
As aparameter to a C function:
void SomeFunctionThatTakesABlock(returnType (^blockName)(parameterTypes));
As atypedef:
typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};
网友评论