Objective C block syntax (method signature, anonymous block)
It's common to define method signature and provide an anonymous call-back block to it. (this is common when you are doing completion handler). The syntax is sort of weird, though it's much better when you get used to it. There are two syntaxes you need to be aware of when dealing with blocks. First is defining interface, and second is implementing that interface with a block.
Suppose the interface (loosely, structure) of a function is like this:
BOOL somefunction(NSURLRequest *request){
...
}
Obviously you don't get this kind of syntax in objective C, but it's much easier to translate things from what we are familiar with rather than building it up from the scratch. So the interface of the above callback function can be written as the following in objective-C:
BOOL(^)(NSURLRequest *)somefunction
Then, it should be clear that the completion handler below
is equivalent to a structure of a function:
void completionHandler(TransactionResult, NSSTring *, NSStringDictionary){
...
}
Since the method 'generateRequestHPP' above requires such a method block for completionHandler parameter, we need some sort of a way to provide that. Let's go back to our initial example. A block that satisfies the following interface:
BOOL(^)(NSURLRequest *)somefunction
Would just be
^BOOL(NSRequest *request){
return YES;
}
And equivalently, you would pass a completion handler that looks like below for generateRequestHPP:
[[HPP getInstance] generateRequestHPP:requestMap url:url attemptNumber: 0 completionHandler:^(TransactionResult result, NSString *string, NSDictionary *_Nullable response ) {
if (result != TransactionResultSucceeded) {
transactionResultDetails = [TransactionResultDetails new];
// transactionResultDetails.transactionResult = result;
// [DataUtils FillDetails:transactionResultDetails fromResponse:response];
[[HPP getInstance] releaseSession];
[self resultsPageBackButtonClicked];
}
else {
if([currentViewController isKindOfClass:[PxPayView class]]) {
PxPayView *pxPayView = (PxPayView *)currentViewController;
[pxPayView loadUrl:string];
}
}
}];
noting that void doesn't necessarily get included since it's unnecessary.