Block Variables
Blocks are not limited to living in-line in method or function calls. You can have variables, whether local, global, or instance, that point to blocks. The syntax is like standard C function pointer syntax but using a caret instead of a star:
void (^blockPtrVar) (NSString *arg) = ^(NSString *arg) { NSLog (@"%@", arg); };
The name of the variable is blockPtrVar. The block returns nothing (void) and takes a single NSString argument.
Invoke it like a function pointer:
blockPtrVar (@"hello");
which prints “hello.”
Things become more readable when you use a typedef:
typedef void (^BlockType) (NSString ...
Get Advanced Mac OS X Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.