Introduction to Blocks in Objective-C – Part 1



Beginning with iOS 4.0, Apple introduced blocks, which look and operate much like C functions. However, blocks offer many interesting capabilities beyond functions as you know and love them today.

A block is really nothing more than a chunk of code. What makes them unique is that a block can be executed inline as well as passed as an argument into a method/function. Blocks can also be assigned to a variable and called as you would a C function – this, along with a few other topics will be covered in this post.

Block Variable

To define a block variable, the ^ operator is used. Let’s look at a simple block that will return YES/NO based on whether the passed in integer is an even or odd value:

The diagram below describes each section of the block:

To call the block look similar to a C function call:

The output for the log statement is:

-101 is not an even number

Blocks and Variable Scope

Notice in the block below that the code in the body references the variable ‘price’ which is defined outside the block.

The output for the log statement is:

Ordering 10 units, final price is: $19.90

Let’s change the price and run the block again:

Seems changing the price should update the return value from the block, however, during the block definition the price variable is set to a const, so the output remains the same as before:

The output for the block, with the price variable updated is:

Ordering 10 units, final price is: $19.90

Using __block Storage Modifier

To allow a variable defined outside a block to be mutable, apply the __block storage type modifier:

The output for the block, with the price variable updated is:

With block storage modifier – Ordering 10 units, final price is: $9.90

Typedef and Blocks

Before we get to typedef, let’s look at two different ways to define a block variable. In the examples above, the block, parameters and body are defined in one fell swoop:

This is equivalent to creating a block definition:

and at some point later, defining the body:

This above is important, as it may make sense at some point to have the same signature for two distinct blocks. This is where the typdef comes in – a best practice is to use typedef if the same block signature is used for two or more unique operations:

For example, below a typedef is defined for a block that accepts two objects and returns an NSComparisonResult:

NSComparisonResult is an enum that can be used to indicate the ordering of items (ascending, descending, same).

With the typedef in place you could now write two blocks, both accepting objects, however each performing a different comparison:

Here is another block with the same signature, however, there you could change up the logic for comparing the objects:


This post covered the basics of blocks, including block variables, the __block storage modifier and using typedef to define a block.

In an upcoming post we’ll look at a few more interesting aspects of blocks, such as passing a block as a parameter to a method, enumeration with blocks, as well as using recursion in a block.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s