Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C block parameters

Say we have this block:

int (^aBlock)(BOOL) = ^(BOOL param) { ...

My current understanding of this is: the first int is the return type, (^aBlock)(BOOL) gives the name of the method and the type of its parameter, and = ^(BOOL param) is the parameter's name inside the block ... plus the parameter's type again?

Why is the syntax such that we have to list the parameter type twice? Could the two types ever be different?

like image 333
chm Avatar asked Oct 15 '12 03:10

chm


1 Answers

Why is the syntax such that we have to list the parameter type twice?

The block is designed in this way, and so you can do it like this:

int (^aBlock)(BOOL);

aBlock = ^(BOOL param) {
  ...
};

It just likes

- (int)aMethodWithParam:(BOOL)param;

- (int)aMethodWithParam:(BOOL)param {
  ...
}

Could the two types ever be different?

Nope, and what's more, the order of the types should be the same, i.e.:

int (^aBlock)(BOOL, NSString*) = ^(BOOL param, NSString *aString) {
  ...
};

And here's a clear figure for block:

Image

like image 189
Kjuly Avatar answered Sep 30 '22 14:09

Kjuly