#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 Probe auf Zeiger ist mein Code in Objective-CWie Funktion implementieren in schnellen
- Rückruf Block "myMethod1" (Antwort) ist mit Verweis/gespeichert "Datenblock" Eigentum.
- dann "DataBlock" von "myMethod2" aufrufen.
- seit "Datenblock" Verweis auf "myMethod1" Block namens "Antwort", ich werde Rückruf in "myMethod1" bekommen, bitte schauen Sie sich das Code-Snippet (ähnlich wie Zeiger).
- gleiche Sache, die ich in swift implementieren möchte. Ich habe versucht, dies zu implementieren, indem ich Schließungen schnell einsetzte, aber nicht bekam.
Danke für die Antwort. –