Reputation: 3832
I am relatively new to programming and there is one thing which I can not manage to wrap my hand around. That is, what are blocks and why/when would you use them? What is the difference between a block and a method? To me, they seem like they kind of do the same thing.
Can some explain this to me?
Yes, I did spend hours on Google before finally coming here to ask.
Upvotes: 4
Views: 1286
Reputation: 2142
Suppose you want to perform an operation like the animation on view and wanted to be notified after completion. Then you had to write this code:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:context:)];
[UIView commitAnimations];
But you need to few lines of code if you are using a block like below:
[UIView animateWithDuration:2.0 animations:^{
// set up animation
} completion:^{
// this will be executed on completion
}];
Hope you are clear now about the use of the block.
Upvotes: 5
Reputation: 520
Upvotes: 4