Created
May 24, 2012 19:39
-
-
Save jacob414/2783763 to your computer and use it in GitHub Desktop.
With blocks and varargs Objective C might not be as bad after all
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
#include <stdarg.h> | |
#import <Foundation/Foundation.h> | |
@interface Cls : NSObject {} | |
-(int) takes_block: (int)limit withBlock:(int (^)(int first, ...))block; | |
@end | |
@implementation Cls | |
-(int) takes_block: (int)limit withBlock:(int (^)(int first, ...))block { | |
return block(55, @"Hello block, this is your caller"); | |
} | |
@end | |
int main(int argc, char *argv[]) | |
{ | |
Cls *obj = [Cls alloc]; | |
int result = [obj takes_block:5 withBlock:^(int number, ...) { | |
va_list args; | |
va_start(args, number); | |
NSLog(@"comment: %@", va_arg(args, NSString*)); | |
va_end(args); | |
return number * 23; | |
}]; | |
NSLog(@"Result from takes_block: %d", result); | |
return 0; | |
} | |
// Run like: | |
// $ gcc -framework Foundation -ObjC blocks.m -o blocks.app && ./blocks.app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can I make the block separate from the method and just pass only block name to the method