Objective-C Block


In Objective-C, you can pass a primitive type variable, a pointer or a object to a function. You can also pass a piece of code, which can be regarded as a “function”, to a function. This is called block in Objective-C. Block looks complicated at first, however, once you start to analyze the syntax, you will find out that the syntax is really intuitive and straightforward.

Here are two great posts that I want to recommend to you.

Also, here is a great sample code from GitHub

Since I am really familiar with C, I also want to summarize the block syntax by talking about C syntax first.

Objective-C Block Syntax and C

I believe that one of the best ways to learn new ticks is to relate those new ones to some old ones that I have already knew. I am familiar with C, so I’ll start with C.

There are four modifiers that are extensively used in Objective-C block: ^, *, [], {}. Fortunately, only the first one is not included in C. The others are really common modifiers in C. If you are a C person, you will find them familiar.

It is important to keep how to read a declaration in mind. The first thing is to find the variable name or identifier. Then, your should go right until you can’t and the start over from the left of the variable. A declaration can only have 1 basic type and it’s the left most word of the declarator. Another important thing is that [] and () have higher precedence over * and ^.

Here is a list of C declarators

1. Basic declarator.

a is the variable name, and int is the type.

2. The pointer modifier

3. The array modifier

4. The function identifier.

The function name is a, and the return type is int.

5. Array of pointers.

Since [] has higher precedence than *, a is an array first. Then find the basic type which is the pointer to int. Thus, a is an array of points to int.

6. Pointer to array.

Again, because of the precedence rule, if you want a pointer to array, you need a pair of parenthesizes like this.

7. Function takes array as parameter

You can’t have an array of function and a function cannot return an array of a function. However, A function can take an array as argument.

8. Function returns pointer

As () has higher precedence than *, f is a function first and returns an int.

9. Pointer to a function

Similar to the relationship between array and pointer, if you want a pointer to a function, you need to use parenthesizes.

The Block Pointer ^

The block pointer ^ is similar to a pointer modifier. Since the block is similar to a function, you declare a block the same way you would declare a pointer to a function. Just replace the * in int (*f)() with the block pointer ^.

b is the block pointer to a function that returns an int.

A block can also have parameters.

Abstract Declarator

Declarator = Abstract Declarator + Identifier

The abstract declarator for the block above will be int (^)(long a, long b) or int (^)(long, long)


How to Use Blocks

As a local variable



Here is an example which uses block to calculate addition. You will the output of 300.

As a property

As a method parameter

As an argument to a method call



As a typedef