For some reason I can’t get it in my head how to declare a block in Objective-C. Maybe its syntax is too complicated or my brain is blocking this style of block declaration.
There are many ways you can declare a block.
As a property,
1 |
@property (nonatomic, copy) returnType (^blockName)(parameterTypes); |
Or as a method parameter,
1 |
- (void)methodThatUseABlock:(returnType (^)(parameterTypes))blockName {} |
I strongly suggest to use block instead of delegates is a cleaner way to code, especially with delegates.
Or a local variable,
1 |
returnType (^blockName)(parameterTypes) = ^returnType(parameters) {}; |
Or as a typedef,
1 2 |
typedef returnType (^TypeName)(parameterTypes); TypeName blockName = ^(parameters) {} |
Source Reference: iOS Developer Library