Skip to content

Instantly share code, notes, and snippets.

@CastIrony
Created July 16, 2010 21:18
Show Gist options
  • Save CastIrony/478931 to your computer and use it in GitHub Desktop.
Save CastIrony/478931 to your computer and use it in GitHub Desktop.
#define BLOCK(name, return, ...) return(^name)(__VA_ARGS__)
#define BLOCKTYPE(return, ...) return(^)(__VA_ARGS__)
// Example Usage:
// To declare a block inside a function/method:
//
// BLOCK(foo, int, BOOL) = ^(BOOL bar) { return bar ? 4 : 3; }
//
// NSLog(@"%d", foo(NO)); // "3"
// To write a method that returns one kind of block (which returns a NSDictionary*, and takes an NSArray* and a float) and takes
// another block (which returns an NSString* and takes an int and a bool) and you would write:
//
// -(BLOCKTYPE(NSDictionary*, NSArray*, float)) myMethodWithBlock:(BLOCKTYPE(NSString*, int, bool))myBlock
//
// Obviously that's a mess, and highly unlikely, but it's an example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment