Skip to content

Instantly share code, notes, and snippets.

@jacob414
Created May 24, 2012 19:39
Show Gist options
  • Save jacob414/2783763 to your computer and use it in GitHub Desktop.
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
#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
@Vkagde
Copy link

Vkagde commented Sep 1, 2017

Can I make the block separate from the method and just pass only block name to the method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment