Introduction to iOS Blocks (revisited)

OK, my first real blog back, and I thought i’d revisit iOS Blocks. I did an article on this a while back, but recently came across a fantastic tutorial set from [iOS Developer Tips]Introduction to Blocks in Objective-C, which are split into two parts, posted by John Muchow. I found his articles to be the most concise and well-explained, in terms of Blocks, better than the WWDC Video I saw of the concept. With Apple gradually making all of it’s API’s Block-oriented, this is a vital and worthwhile read.

You can find the articles at :

 A brief overview of the two articles are below:

 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:
// Defining a block variable BOOL (^isInputEven)(int) = ^(int input) { if (input % 2 == 0) return YES; else return NO; };
The diagram below describes each section of the block:
To call the block look similar to a C function call:
// Call similar to a C function call int x = -101; NSLog(@"%d %@ number", x, isInputEven(x) ? @"is an even" : @"is not an even");
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.
float price = 1.99;   float (^finalPrice)(int) = ^(int quantity) { // Notice local variable price is  // accessible in the block return quantity * price; };   int orderQuantity = 10; NSLog(@"Ordering %d units, final price is: $%2.2f", orderQuantity, finalPrice(orderQuantity));
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:
price = .99; NSLog(@"Ordering %d units, final price is: $%2.2f", orderQuantity, finalPrice(orderQuantity));
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:
// Use the __block storage modifier to allow changes to 'price' __block float price = 1.99;   float (^finalPrice)(int) = ^(int quantity) { return quantity * price; };   int orderQuantity = 10; price = .99;   NSLog(@"With block storage modifier - Ordering %d units, final price is: $%2.2f", orderQuantity, finalPrice(orderQuantity));
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:
BOOL (^isInputEven)(int) = ^(int input) { if (input % 2 == 0) return YES; else return NO; };
This is equivalent to creating a block definition:
BOOL (^isInputEven)(int);
and at some point later, defining the body:
isInputEven = ^(int input) {
Advertisements
This entry was posted in General, iOS and tagged , . Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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