- ( void )myMethodTakingPredicate: ( BOOL ( ^ )( int ) )predicate
This is how it goes, for example...
[self smartBlocks:@"Pen" youSmart:^(NSString *response) {
NSLog(@"Response:%@", response);
}];
- (void)smartBlocks:(NSString *)yo youSmart:(void (^) (NSString *response))handler {
if ([yo compare:@"Pen"] == NSOrderedSame) {
handler(@"Ink");
}
if ([yo compare:@"Pencil"] == NSOrderedSame) {
handler(@"led");
}
}
http://fuckingblocksyntax.com
As a method parameter:
- (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;
Another example (this issue benefits from multiple):
@implementation CallbackAsyncClass {
void (^_loginCallback) (NSDictionary *response);
}
// …
- (void)loginWithCallback:(void (^) (NSDictionary *response))handler {
// Do something async / call URL
_loginCallback = Block_copy(handler);
// response will come to the following method (how is left to the reader) …
}
- (void)parseLoginResponse {
// Receive and parse response, then make callback
_loginCallback(response);
Block_release(_loginCallback);
_loginCallback = nil;
}
// this is how we make the call:
[instanceOfCallbackAsyncClass loginWithCallback:^(NSDictionary *response) {
// respond to result
}];
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