I need to do a repeating task with the result of an http request and so would like to store the declaration of the completion hander in a variable (or somehow declare it as a function that can be passed in).
Example code:
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// carry out some action
}];
I would like to be able to write
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler: standardHandler ];
Where standardHandler contains a block function.
Is this possible?
I'm quite new to Objective C so sorry if this is an obvious question.
Sure:
void (^completionHandler)(NSURLResponse *, NSData *, NSError *) = ^(NSURLResponse *response, NSData *data, NSError *error) {
// carry out some action
};
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:completionHandler];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With