Tag Archives: Objective-C

What Are Blocks In Objective-C?

If you take a look at the new documentation (since iOS 4) you’ll find some strange notation for some of the API.  For instance, if you look at the API for NSArray you’ll find some methods like this:

- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block;


Even if you’ve been working with Objective-C for a while you may be thinking WTF to yourself. That is odd notation, what on Earth is and how do you use it?

It’s Called a Block Captain

Yes that’s right, the method above is taking advantage of a feature that’s been available since iOS 4 called Blocks.  Blocks are something that some other programming language have had for a while now.

What Are Blocks For, Exactly?

Anyone who has programmed for a while know that we need different ways to keep code organized.  Usually, we will define areas of code called functions or methods that will perform a particular action based on a set of parameters that you supply to the function.

Blocks are an additional way to organize code and so, in a way, they are a bit like functions.  However, Blocks have some special properties that functions don’t have.

Blocks Are Objects

This is a neat feature.  Blocks have the same properties as an Objective-C object so if you can move these Blocks around much like other objects.

You can also use Blocks as parameters to functions which is why you see this new odd notation in some of the Foundation API in iOS 4.

Blocks Save State When Declared Because They’re Closures

When a Block is declare, the current state of all the variables around the Block are remembered.  So if a = 5 when the Block is declared then a will equal 5 when the Block executes even if the state of a has changed when the Block finally executes.

Things that save the local context like this are called closures and so Blocks in Objective-C are examples of closures.

Why Should I Mess With Blocks?

So, yes it’s true that this new entity can be confusing at first and frankly using a Blocks may cause you more agitation than their worth.  I’ve seen some passionate reasons in the past to adopt new techniques like this.  The bottom line for me is whether I end up in situations where I think something like this can be useful, but either way you should have an idea of what these things are.

That is because there are two situations in which you will need to use this Blocks: if you are planning on using Grand Central Dispatch to perform multithreading and if you need to use some of the new API that Apple is providing in iOS 4 and iOS 5.

Examples Of Blocks

Here are some examples of what Blocks look like.  What will clue you into the fact that a Block is being used is if you see the ^ symbol.

Using New API That Requires Blocks

NSArray *cards = [NSArray arrayWithObjects:@"Jack", @"Queen", @"King", @"Ace", nil];
[cards enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
     NSLog(@"%@ card at index %d", object, index);

Example Of Using Blocks As Objects

NSDate *date = [NSDate date];
void (^now)(void) = ^ {
     NSLog(@"The date and time is %@", date);

Excellent Tutorial On How To Use Blocks

Both of these examples came from a blog post on The Pragmatic Studio blog.  They have a very good and concise introduction to the syntax of Blocks and go into how to use Blocks.  I definitely recommend checking this out.