August 2011
Intermediate to advanced
552 pages
23h 48m
English
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 ...
Read now
Unlock full access