Skip to content

Instantly share code, notes, and snippets.

@claybridges
Created March 19, 2014 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claybridges/9646508 to your computer and use it in GitHub Desktop.
Save claybridges/9646508 to your computer and use it in GitHub Desktop.
Adapt one block type to another, in this case to get rid of commonly unwanted parameters. In this SO answer: http://stackoverflow.com/a/22513387/45813
@class TKState, TKStateMachine;
typedef void (^LongStateBlock)(TKState *state, TKStateMachine *stateMachine);
static inline LongStateBlock Adapter(void(^block)()) {
void(^heapBlock)() = [block copy]; // forces block to be on heap rather than stack, a one-time expense
LongStateBlock longBlock = ^(TKState *s __unused, TKStateMachine *sm __unused) {
heapBlock();
};
// this is the non-ARC, MRR version; I'll leave ARC for the interested observer
[heapBlock release];
return [[longBlock copy] autorelease];
}
- (void)takesLongStateBlock:(LongStateBlock)longBlock
{
if (longBlock) longBlock(nil, nil);
}
- (void)yourRandomMethod
{
[self takesLongStateBlock:^(TKState *state, TKStateMachine *stateMachine) {
NSLog(@"Gratuitous paramaters, AAAAHHHH!");
}];
[self takesLongStateBlock:Adapter(^{
NSLog(@"So, so clean.");
})];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment