views:

66

answers:

2

What is the advantage of using blocks over normal methods and functions in Objective-C? I've read the documentation, but I can't find specific uses of blocks instead of other language features.

I'm sure that I've missed something, so could someone explain the advantages of blocks in a simpler way than the existing documentation?

A: 

Blocks are a way of wrapping up a piece of code and effectively storing it for later use. A block is commonly used in place of a call back function. Newer APIs in the iPhone SDK use blocks this way. The API will take a "block" of code that it will run on completion.

It saves you having to create your own threads and maintain the state of each thread, manage locks, setup autorelease pools etc.

When used with the Grand Central Dispatch (GCD) API blocks can be run on queues and entire portions of code can be made to run asynchronously with very little effort, but still keeping the robustness that is required for multithreaded code.

Jasarien
I understand it's importance when it comes to use with GCD but apart from this i am finding it difficult to understand the terms like callback and many things like this.The documentation has used complex terminilogy so i am here if someone can please tell me the real useof blocks in beginers language.Please keep it simple and some code can be explained,that will be great...Thanks
Ajay Pandey
@Ajay Pandey - What specific language is giving you trouble? A callback is a method or function that is triggered in response to something happening within the application or system. Blocks can let you define actions that take place in response to an event, but rather than have you write a separate method or function, they allow you to write the handling code right where you set up the listener for that event. This can save a mess of code and make your application much more organized.
Brad Larson
that's what i was understanding but now i got confirmation.Thanks.
Ajay Pandey
+2  A: 

I like Apple's "A Short Practical Guide to Blocks" as an introduction to the concept.

In addition, almost all of the resources pointed to in response to the question "Suggested resources for learning about blocks in Snow Leopard" would apply here.

On top of the resources there, I recommend the articles "Cocoa for Scientists (Part XXVII): Getting Closure with Objective-C" and "Cocoa for Scientists (XXXIII): 10 Uses for Blocks in C/Objective-C" by Drew McCormack, as well as "Programming with C Blocks" by Joachim Bengtsson.

The WWDC 2010 videos for sessions 206 - "Introducing Blocks and Grand Central Dispatch on iPhone" and 211 - "Simplifying iPhone App Development with Grand Central Dispatch" are well worth watching too.

Brad Larson
i am not looking for blocks in snoe leopard but for iPhone and iPad and as far as first link is concerned i am not too impressed with the way it has been explained there.Can you suggest me some startup codes for blocks.That will be great help..
Ajay Pandey
@Ajay Pandey - There is no functional difference between blocks in Snow Leopard and those on iOS 4.0. The language syntax is the same and Grand Central Dispatch is the same, so all of the links there apply here. Asking for example code for blocks is like asking for example code for methods or functions. It's a core language feature, so it's difficult to create a really basic example.
Brad Larson
@Ajay Pandey - I added a few more resources in my answer that may be helpful.
Brad Larson
Thanks a lot for help.
Ajay Pandey