Ticker

6/recent/ticker-posts

Block Codes in Objective-C

What is Block Codes?

Generally blocks are well know as completion handlers or call backs. These are similar like delegates in Objective-C but the use case is different.

A block is a self-contained, autonomous code fragment, existing always into the scope of another programming structure, as for example the body of a method. The code on the block can “interact” with the world out of it, but what takes place in the block is not visible to the scope out of it. Also, blocks can modify the values of variables being out of its body using a special way (we’ll see more later).

Blocks have two great features:
They can be executed in a later time, and not when the code of the scope they have been implemented is being executed.
Their usage leads eventually to a much cleaner and tidier code writing, as they can be used instead of delegate methods, written just in one place and not spread to many files.
Blocks offer a nice solution for creating callbacks instead of delegates, as they are declared and implemented directly to the point of the code where they are invoked. Using blocks, there is no need to adopt protocols, or implementing delegate methods that lead to much more code in a class. The best thing though, is that the callback procedure can directly access and use any variables existing locally in the scope where the block has been defined, so the need of passing data like we do in callback methods is eliminated. However, blocks cannot totally replace delegates as they cannot be used for every purpose. The best practice is to make combination of both, and that can be achieved both by following some simple rules and using the experience obtained in time.

Demo App:

Let's start creating demo application to understand blocks clearly.

Make sure the details of project name, identifier and company name


Before going to write all the code let's see the declaration syntax patten of block
returnType(^BlockName)(Parameters)
This is quite similar to a C function declaration, with one great exception: The special caret cap (^) symbol, which indicates that the declared object is a block.

ReturnType: Any data type Objective-C supports, or void if the block returns nothing.
^blockName: Always remember that the block name is preceded by the ^ symbol. The block name can be any string you like, just like you name any other variable or method. Remember that both the ^ and the block name are enclosed in parentheses ().
Parameters: The list of parameters you want to pass on the block, just like you do when declaring methods. However, keep in mind an important notice: When you pass no parameters to block, then the void keyword should be always set. The arguments list must also be enclosed in parentheses.

Let's see the few examples of declaring blocks

- int (^StringResponse)(NSString *param1, int param2);

- void (^UserName)(NSString *myName);

- NSDate *(^TodayDate)(void);

- void (^AllVoid)(void);


- NSString *(^FullUserName)(NSString *firstName, NSString *lastName);

Let's go to block implementation

^(Parameters){
    
    //block body ...
    
    return ReturnValue (or nothing if the block return type is void)
};

Visually you can observe in the below image


To be continued with real-time example.....

Post a Comment

0 Comments