Skip to content

Instantly share code, notes, and snippets.

@hashier
Created May 13, 2014 17:42
Show Gist options
  • Save hashier/5eeb922fb3c0ce8be5ae to your computer and use it in GitHub Desktop.
Save hashier/5eeb922fb3c0ce8be5ae to your computer and use it in GitHub Desktop.
How to call blocks?
#import <Foundation/Foundation.h>
// 2nd class
@interface DownTheLine : NSObject
@end
@implementation DownTheLine
-(void)deepDown:(void (^)(int var))completionHandler {
completionHandler(4);
}
@end
// 1st class
@interface FirstRecv : NSObject
@end
@implementation FirstRecv
-(void)first:(void (^)(int var))completionHandler {
DownTheLine *down = [[DownTheLine alloc] init];
[down deepDown:completionHandler];
}
-(void)second:(void (^)(int var))completionHandler {
DownTheLine *down = [[DownTheLine alloc] init];
[down deepDown:^(int var) {
completionHandler(var);
}];
}
@end
#define NUM 2000
// main
int main(int argc, char *argv[]) {
@autoreleasepool {
FirstRecv *a = [[FirstRecv alloc] init];
CFAbsoluteTime startTime;
__block int counter;
startTime = CFAbsoluteTimeGetCurrent();
counter = 0;
for (int i = 0; i < NUM; ++i) {
[a first:^(int var) {
//NSLog(@"1st run: %d", var);
counter += i;
}];
}
NSLog(@"%.2fms", 1000.0*(CFAbsoluteTimeGetCurrent() - startTime));
startTime = CFAbsoluteTimeGetCurrent();
counter = 0;
for (int i = 0; i < NUM; ++i) {
[a second:^(int var) {
//NSLog(@"2nd run: %d", var);
counter += i;
}];
}
NSLog(@"%.2fms", 1000.0*(CFAbsoluteTimeGetCurrent() - startTime));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment