Block is heavily used in objective-c APIs. If you want to write concurrent and responsive interface, you will need blocks and grand central dispatch. Blocks also bring many goods form functional programming to cocoa. It is just awesome.
However, when I first learn blocks I found the syntax confuses me a lot. This is why I wrote this article. Hope this article can help people who have the same problem as I did.
Declare a block variable
Block syntax inherited form C’s function pointers. To declare a block variable, you write:
1 2 3 4 5 6 7 |
|
It is similar to function pointer:
1 2 3 4 5 6 7 |
|
Block literal syntax shortcut
Block literal can be written in various ways:
1 2 3 4 5 6 7 8 9 10 |
|
Anonymous block
You don’t need a block variable to use a block. A block without a block variable is called anonymous block.
1 2 |
|
Many objective-c methods accepts anonymous block:
1 2 3 4 |
|
Compare with function pointer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
What does above mean? Well, if you want to pass a callback function to
elsewhere, sometimes you might also need to pass data. To do that, you first
pack your data into a struct
, and use a void
pointer points to it. Then you
pass the callback function and the void
pointer to the function. Finally you
dereference the void
pointer back to the struct
.
With block, all variables in it are captured. You no longer need to do that type casting hack to pass data.
1 2 3 4 5 6 7 |
|
Typedef
We can use typedef
to define a reusable type:
1 2 3 |
|
Type cast
As other types, you can also type cast a block. The syntax is a little weired, though.
1 2 3 4 5 6 7 |
|
Block in Objective-C class
property
Block in objective C is quite trivial:
1 2 3 |
|
Accessors and method arguments
However, it’s strange in method declaration and accessors.
1 2 |
|
The syntax is weird because Apple uses type cast syntax as type declaration syntax.
This is now the only way to use anonymous type in Objective-C method argument
instead of using typedef
. This syntax won’t work in other places, either.
Other syntaxs
Array
You can define a chunk of blocks like so:
1 2 3 4 |
|
Nested blocks
Nested block syntax is ugly:
1 2 3 4 |
|
Readability of nested block without typedef
is so horrible. typedef
is
strongly recommended.
That’s all for block syntax! There are still topics to discuss like memory management and grand central dispatch. I’ll discuss them in next few posts.