#import "ViewController.h"
@interface ViewController ()
//Declare block as property
@property (nonatomic, strong) void (^dataBlock)(BOOL success);
@end
@implementation ViewController
- (void) myMethod1:(void (^)(BOOL success))response {
//Here data block holds the reference to response block
_dataBlock = response;
}
- (void) myMethod2 {
//Check for _dataBlock and invoke it.
if (_dataBlock) {
_dataBlock(YES);
}
}
- (IBAction) buttonClick {
//Call for myMethod1 and call back block is invoked in myMethod2
[self myMethod1:^(BOOL success) {
if (success) {
NSLog(@"Im Done");
}
}];
}
@end
Above sample is my code in Objective-C
- Callback Block of "myMethod1"(response) is having reference/stored to "dataBlock" Property.
- then invoke "dataBlock" from "myMethod2".
- since "datablock" have reference to"myMethod1" block named "response", i'll be getting call back in "myMethod1", please look at the code snippet (similar to function to pointer).
- same thing i want to implement in swift. I have tried implementing this in swift using closures, but not getting it.