Blocks in objective C – part 1

This content has 10 years. Please, read this page keeping its age in your mind.

With the release of iOS4 blocks became available in objective C. It is not something new, they are exist in other languages like Java’s anonymous classes and C# lamdas. I have to admit that because of the syntax and the way they operate, they have become a frightening tool for the beginner developers.

There are many tutorials around, I’m not going to write something unpublished. My bid is to write something that will help me to understand better the blocks concept and maybe will help some of you too.

Blocks: An Objective-C class defines an object that combines data with related behavior. In other words it is a block of inline statements that can be passed around to other functions as if it were an object.

To define a block you use the caret (^) symbol. For example,

    NSLog(@"A simple block");


^(int num1, int num2) {
        return num1+num2;

We could have written
^int(int num1, int num2) { ...
including the return type but this is optional.

So in general block definition follows this rule:
^return_type(param_type param_name, param_type param_name, ...) { ... return return_type; }

Blocks are declared like methods, so we have a return type and the arguments.

void (^nameOfTheBlock)(void);
int (^anotherBlock)(int, int);

Be careful with the position of the braces and the caret(^) symbol.The above declaration tells us that the anotherBlock is a variable and refers to a block that returns and integer and it takes two integers as arguments (The syntax is the same with the C function pointers).

So, having a variable of type anotherBlock we can assign any block that satisfies the restriction about the return value and the arguments. As example,

anotherBlock = ^(int num1, int num2) {
        return num1+num2;

or instead of using the variable we could have written also:

int (^anotherBlock)(int, int) = ^(int num1, int num2) {
        return num1*num2;

both are correct.

A parameterless block (is a block that doesn’t take any parameters) looks like that

nameOfTheBlock = ^{
        NSLog(@"This is a block");

Be careful, the statement must be terminated by a semi-colon after the closing curly bracket.

Now that we have declared and assigned the block we can invoke it as simple as we do with functions

nameOfTheBlock(); // and "This is a block" will be printed


int sum;
sum = anotherBlock(3,5);
NSLog(@"The sum is %d", sum); //"The sum is 8" 

Another thing you need to know about the syntax of blocks is that you can use typedef to make your life easier and the code easier to read. Bearing in mind that he caret (^) behaves in a similar manner to the asterisk before a pointer (e.g., int *aPointer) you will not get panicked. So declaring a block could be easily done by using typedef.

We can write

typedef int (^operationBlock)(int, int);

by that, we mean that operationBlock (this is the name) represent a block which returns an int and take two integers as arguments.

So we can use it like this

int add(int num1, int num2) {
    return num1+num2;

int multiply(int num1, int num2) {
    return num1*num2;

operationBlock addBlock = add; 
int num3 = addBlock(4,5);
NSLog(@"The result is %d", num3); // Output: "The result is 9"

operationBlock multiplyBlock = multiply;
num3 = multiply(2,3); 
NSLog(@"The result is %d", num3); // Output: "The result is 6"

or we can use typedef to define properties like that

typedef void (^XYZSimpleBlock)(void);
@interface XYZObject : NSObject
@property (nonatomic, copy) XYZSimpleBlock blockProperty;

without typedef, it would have been

@property (nonatomic, copy) void (^blockProperty)(void);

1. Objective-C Succinctly: Blocks
2. Blocks Programming Topics
3. Getting Started with Blocks in Objective-C
4. Understanding typedefs for function pointers in C: Examples, hints and tips.
5. The ugly side of blocks: explicit declarations and casting.
6. Working with Blocks
7. Blocks
8. How To Use Blocks in iOS 5 Tutorial – Part 2

One thought on “Blocks in objective C – part 1

  1. What’s Going down i’m new to this, I stumbled upon this I have found It absolutely useful and
    it has aided me out loads. I’m hoping to give a contribution & aid different customers like its
    helped me. Good job.

Comments are closed.