Created
March 19, 2014 17:15
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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