Objective C Blocks: The Essential Building Blocks

c sharp charObjective C is a comparatively lesser known language compared to its cousins, C and C++. Objective C is, however, a very powerful language to work with. Unlike C, it is an object oriented programming language. This allows it to provide functionality needed by modern day apps and games. However, Objective C also supports structural programming techniques, so you can choose OOP techniques or structural programming techniques (writing functions instead of classes) at different times. The language has been derived to a large extent from C. Because of its roots, it’s possible to run C programs, after small modifications, in Objective C. If you have any programming experience with C or C++, Objective C will be an easy language for you to learn. You can still use any of the old programs you’ve written in C , on Objective C.

Why bother learning Objective C at all? Because it is the language that is used to make the iOS operating systems. It is also the language that is used for Apple’s Cocoa and Cocoa touch framework- you can use it to develop apps for iTunes! If you’re interested in learning Objective C, sign up for our introductory course. We teach you the basics of the language as well as the advanced stuff. Take the course and you will be writing your own apps in no time!

Using Blocks in Objective C

In this basic tutorial, we’re going to show you how you can use blocks in Objective C. They are a fundamental part of Objective C (and a lot of other languages, in fact). You’ll be hard pressed to find a program without a block in it.

So what are blocks exactly? In Objective C, blocks are code statements grouped together inside two curly braces { }. A typical block consists of a bunch of statements and declarations that work to accomplish a task. A block can also be empty, however. Objective C treats a block like an object. A block can contain code that can be used by methods or functions. Generally, we pass values to methods or functions with the help of blocks.

The syntax to declare a block is as follows:

^{
declarations
and
statements
}

Please note that the caret ^ key is necessary while declaring a block.

In Objective C, blocks are very similar to functions. Blocks are declared the same way that you would define a function. They also work the same way as functions do – just like a function can be called, a block can be called as well. To learn more about functions, classes and object oriented concepts, you can take a look at some of the other tutorials we’ve written on the topics. You can also sign up for our Objective C course and learn everything in a logical fashion.

Examples of Blocks in Objective C

You can use a variable to store a block you have created. Later, you can just call this variable to invoke the block you wrote. This saves you from having to type the block again.

For example, let’s declare a variable called “exampleBlock” and store a block inside it.

void (^exampleBlock) (void);
exampleBlock = ^{
NSLog (@”This is an example of a block”);
};
exampleBlock() ;

Output:

This is an example of a block

Here, we used the variable exampleBlock to store a block that had a print statement in its body. At the end of the program, we called the block with “exampleBlock()”. Notice that when we assign a variable to a block, a semicolon has to be present after the block declaration. Also, we declared a void variable to allow it to store the Block inside it and prevent it from returning any value.

There is another way to write the same program:

void (^exampleBlock) (void) = ^{
NSLog (@”This is an example of a block”);
};
exampleBlock();

Output:

This is an example of a block

All we did here was include the block declaration with the declaration of the variable. It saved us about a line of code. The rest of the program is the same. You can take this course to learn to write your own programs in Objective C.

A Block that Takes Arguments, Performs a Task and Returns a Value

Blocks can be a very useful part of your program. They can store mathematical formula, for example, that you can use anywhere in your program. All you have to do is pass different arguments (input) to get a different result each time.

To create a block that takes arguments, we first need to create a variable to refer to it. The variable, as we explained earlier, can be used to invoke the block later:

int (^areaRectangle) (int, int) ;

The variable we declared in the example earlier was void, but this variable will return an int value.

Now, we need to create a block literal that it can reference to:

^ (int sideLength, int sideBreadth) {
return sideLength * sideBreadth;
}

Every time you want to invoke the block, you will have to supply two values: sideLength and sideBreadth.

Now, how does the finished program look like? We’ll pass two values and find out the length of the rectangle:

int (^areaRectangle) (int, int) =
^ (int sideLength, int sideBreadth) {
return sideLength * sideBreadth;
};
int area = areaRectangle (10, 20);
NSLog(@"The area of the rectangle is %d", area);

Output:

The area of the rectangle is 200

Here, we’ve written a block that takes two arguments, performs a task (finds the area of a rectangle) and returns a value, which is the area of the rectangle. We had to declare a new variable called “area” to hold the area of the rectangle. We also used it to pass two values to the block: 10 and 20.

Blocks can do much more than what we demonstrated here. Getting a good grasp on how blocks work is necessary if you want to be able to write even simple programs in Objective C. Once you’ve mastered the basics, you may want to go over and take this course to help you write your own iPhone/iPad apps!