Blocks are Objective-C’s anonymous functions. They let you pass arbitrary statements between objects as you would data, which is often more intuitive than referencing functions defined elsewhere.
Note: "Blocks are created on stack and hence they will be released as the function call gets over. You need to copy block if you wish to sustain its data."
Blocks are similar to the functions in objective C.
In 3 simple Steps:
1. Declaration
// Declaration a Block int (^calculate)(int a, int b); 2. Definition // Define calculate= ^int(int a, int b) { return a+b; }; 3. Calling NSLog(@"sum is %d",calculate(5,6));
The '^' caret symbol is used to notify the complier that the calculate is a block .
So a block will have
1. Return type
2. Name
3. Variable
If we don't want that our block will have return type or params then we need to replace it with void.
// with return type and params int (^sum)(int a); // without params int (^sum)(void); // without params and return type void (^sum)(void); Now we will work in details with block return type and params. // Either This void (^sum1)(int a)= ^void(int a){ // Some Code .... }; // Or This both will differ is syntax only void (^sum1)(int a); sum1= ^void(int a) { // Some Code ... }; Specifying the return type of a block is always optional . int (^sum2)(void)= ^int(void) { NSLog(@"I am in block 2"); return 4; }; void (^sum4)(void)=^ { NSLog(@"I am in sum 4"); };
==
Blocks are implemented as closures which means they can have access of local and non local variables.
For e.g.
NSString *make = @"Honda"; NSString *(^getFullCarName)(NSString *) = ^(NSString *model) { return [make stringByAppendingFormat:@" %@", model]; }; NSLog(@"%@", getFullCarName(@"Accord")); // Honda Accord
In the above example the "getFullCarName" is the block name and it has NSString as param and return type.
In the definition of the block we skipped the return type as it's purely optional and our param i.e. (NSString * model ) will be a local variable for block.
In the above example the "make" is non local variable and model is local and we are using both.
By default the variables are copied to the block and they are readonly.
If you are willing to change the value of non local variables in the block you have to mentioned this in the variable declaration explicitly.
_ _ block NSString * myString =@"car";
Passing blocks as an argument of a function.
let us suppose a scenario .
There is a model class named Person and its has attributes age and height.
We have defined a block which will take an integer as a param and returns nothing
Here we are having call backs without using delegations. This is the beauty of blocks !!!.
Ref:
1. http://iosdevblog.com/
2. Block memory related: http://www.friday.com/bbum/2009/08/29/blocks-tips-tricks/
Passing blocks as an argument of a function.
let us suppose a scenario .
There is a model class named Person and its has attributes age and height.
We have defined a block which will take an integer as a param and returns nothing
typedef void (^testBlock)(int b); There is a instance function which take block as a parameter. -(void)calculateHeightUsingBlock:(testBlock)heightActivity; In the .M file of this class we have mentioned its details // Here ' heightActivity ' is the name variable for 'testBlock' and if you want to send control back to the calling place you need to call the block as did in the last line 'heightActivity(5)' -(void)calculateHeightUsingBlock:(testBlock)heightActivity { NSLog(@"I am in calculate HEIGHT Block Class and height= %d",self.height); heightActivity(5); // this will execute the body written in the calling place. } Once you write the statement heightActvity(5), The control will go back from where this block is called and the body of block will be executed. // Now calling this block from a view controller - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. Person * newPerson= [[Person alloc]init]; newPerson.height=7; newPerson.age=40; [newPerson calculateHeightUsingBlock:^(int a) { NSLog(@"I am in ViewController Block for Height %d",a); }]; // If you debug your code the control of a function will go back to the new person class as its a normal function call and once the task code of the Person class is executed then the control will be handover to the ViewController class and the Log statement will be executed. }
Ref:
1. http://iosdevblog.com/
2. Block memory related: http://www.friday.com/bbum/2009/08/29/blocks-tips-tricks/
No comments:
Post a Comment