gaussblurinc
gaussblurinc

Reputation: 3692

How to write powerful completion blocks?

I am trying to write a method with completion block:

//typedef head of block
typedef void (^CommonErrors)(NSError *error);

//method with block
-(void)MethodWithString:(NSString*)string onError:(CommonErrors)ErrorBlock;

//somewhere in code:

[self MethodWithString:(NSString*) onError:^(NSError *error)ErrorBlock];

That's all ok, if I double-click on ^(NSError *error)ErrorBlock,
then I will have this code: ^(NSError *error){code}

[self MethodWithString:(NSString*) 
               onError:^(NSError *error){
                   code
               }
];

But every time, when I use this CommonErrors,
I use my template: onError:(CommonErrors)ErrorBlock.
My code everywhere will be like this:

onError:^(NSError *error){
    ErrorBlock(error)
}

Can I do this by defining this template somewhere? Or something similar? Is it possible?

Upvotes: 0

Views: 886

Answers (1)

deleted_user
deleted_user

Reputation: 3805

Actually I just reread your question - The block type will never be used in a consumers method signature. The reason for this is that how would someone else know what parameters to pass without looking at the type?

The behavior you are seeing is by design, the typedef of the block expands into an actual signature.

Apparently this is a code snippets question.

See this http://nearthespeedoflight.com/article/xcode_4_code_snippets

Upvotes: 2

Related Questions