Blocks in objective C – part 3

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

In this part we are going to examine various examples of how we can define methods that accepts blocks. Instead of using them simple as parameters in Cocoa Framework methods, we can design our own methods that accepts blocks. Block as parameters is more flexible than the standard data types. It could be assumed as function pointers but the fact that they can be defined inline makes the resulting code much easier to read.

Let’s start from a simple example. Create a simple class Person

//  Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property int age;

- (void)celebrateBirthdayWithBlock:(void (^)(int))activity;

@end

We defined a method that gets a block as parameter. The block returns nothing and take an integer as argument. Any block of that type will work. Notice that, unlike block variables, this doesn’t require a name for the block, just define the return type and the types of the parameters.

The above header file using the power of typedef will look like:

//  Person.h

#import <Foundation/Foundation.h>

typedef void(^Activity)(int);

@interface Person : NSObject

@property int age;

- (void)celebrateBirthdayWithBlock:(Activity)activity;

@end

Now let’s proceed to the implementation file of the Person Class

//  Person.m

#import "Person.h"

@implementation Person

- (void)celebrateBirthdayWithBlock:(void (^)(int))activity {
    NSLog(@"It's a party!!!");
    activity(self.age);
}

@end

In this point, we define the behaviour of the method, so it will print the message “It’s a party!!!” and then it will execute the block by passing object’s age as parameter.

In main.m we will do the testing

//  main.m

#import <UIKit/UIKit.h>

#import "AppDelegate.h"
#import "Person.h"

int main(int argc, char * argv[])
{
    @autoreleasepool {
        Person *dave = [[Person alloc] init];
        dave.age = 37;
        
        [dave celebrateBirthdayWithBlock:^(int age) {
            NSLog(@"Woot! I'm turning %i", age + 1);
        }];
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
} 

In this stage, we created a Person object named dave. We assigned an age and called the method. In order to call the method we have to add lines of code inside the block and don’t forget ; after the closing square bracket.

The output will be:
2014-05-07 20:27:36.967 test[861:60b] It's a party!!!
2014-05-07 20:27:36.970 test[861:60b] Woot! I'm turning 38

If the above example seems simple to you and you want something more advanced check reference number 3. Stuart answered the question by building gradually an example with a completion block.

In case that you want to study tutorials about blocks and tableView cells, you have to check reference number 4. A relevant issue can be found in Bryan’s answer in reference number 5.

Finally, in reference number 6 you will find a complete tutorial about blocks and communication between an app and a web service. An awesome step by step guide by the main thread blog.

References
1. Objective-C Succinctly: Blocks
2. Blocks
3. Objective-C callback handler
4. Blocks and Table View Cells on iOS
5. How to implement custom callback method but enforce specific parameter
6 Communicating with Blocks in Objective-C
7. How to Use Blocks as Callbacks