Blocks are similar to standard C function having additional support for variable bindings to stack memory and heap memory. Due to this additional support block can maintain a set of state that it can use to impact behavior when executed. Blocks are available with GCC and Clang compiler. They can be used with OS X v10.6 and later, and iOS 4.0 and later.
Block is an inline anonymous function that let you encapsulate chunks of code. You can pass this chunk around like any other objects/variables. Block is denoted by carat (^) symbol.
return type(^block name)(arguments)
double (^add) (float);
double (^add) (float); //add is the name of the block which takes float as an argument
return constantNumber+i //block body
It is also possible to combine the declaration and implementation of block:
Note: Make sure to close the blocks with semicolon.
Calling a Block:
To call a block is as same as to call a function.
eg. float result= add(12);
In the above code if you want to change the value of constantNumber you will do something like this
On running the above code compiler will generate the error “Variable is not assignable(__block specifier is missing)”. This is because blocks capture the local variables in its scope and create its own readonly copy of local variables on the stack which is called closure.
Here the compiler is telling us that we need to use __block specifier. Doing so will make the compiler arrange for it to be placed on the heap so that it can be used by all blocks, and will no longer be declared const inside the block itself.
Thus we can get rid of this error by changing the above code to:
__block float constantNumber=12;
Blocks are a useful alternative to traditional callback functions for two main reasons:
- They allow you to write code at the point of invocation that is executed later in the context of the method implementation.
Blocks are thus often parameters of framework methods.
- They allow access to local variables.
The delegate methods are separate methods than the caller. If delegate methods require to access local variables of caller method, its not possible. Those variables need to be created as class level without any specific use case. Using block specifiers, one can simply access local variables directly.
Thus blocks are good for the below specified reasons:
- Completion handlers
- Notification handlers
- Error handlers
- View animation and transitions
Lets take the example of asynchronous request.
Here you get the response in its respective delegate methods
So your code becomes much scattered at different places. How about you get the response at the very next line of the callback function??
Blocks is the answer.
Lets take one class named ViewController from where you can call the web service.
CallWS *obj=[[CallWS alloc] init];
[obj callWS:@”http://www.google.com” completion:^(NSError *error, NSData *result)
if(nil == error)
NSLog(@”result ==%@”,[result description]);
CallWS is the other class that contains the delegate methods of our NSURLConnectionDelegate and that also assigns the block on response.
@interface CallWS : NSObject
@property (nonatomic, copy) void (^completeGettingListUpdate)(NSError *error, NSData *result);
-(void)callWS:(NSString *) serviceString completion:(void (^)(NSError *, NSData *))completion
//Save the block variable as a class member
NSURL *serviceURL = [NSURL URLWithString:serviceString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serviceURL];
NSURLConnection *serviceConnection = [NSURLConnection connectionWithRequest:request delegate:self];
if (serviceConnection != nil)
responseData = [[NSMutableData alloc] init];
In the above code, a block variable is received as a parameter. Save this variable for calling a block when communication will get completed.
Rest of the code will create an URL object and URLRequest Object. Create the connection with URLRequest object. When the communication will start, any/all of below delegate method will get called.
– (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
– (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
– (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
NSLog(@”Connection failed with error:%@”,[error localizedDescription]);
– (void)connectionDidFinishLoading:(NSURLConnection *)connection
NSLog(@”Connection finished communication:%@”,[error localizedDescription]);
Here at the end of the communication and at the time of error generation, block is called. So the ViewController class which has generated the call will work like synchronous method call. However the communication is handled in asynchronous mode.
This is the power of block!!!
For more details visit the Apple Link.