Like Ruby’s blocks? Use ’em in Objective-C too!

If you are into Ruby you will like the simple yet powerful blocks.

Code Blocks

Blocks in code

While searching for some iOS development info i stumbled into the fact that Objective-C embeds the possibility to use blocks as well! Ain’t live a beauty?

As Apple describes them:

Blocks are objects that encapsulate a unit of work—or, in less abstract terms, a segment of code—that can be executed at any time. They are essentially portable and anonymous functions that one can pass in as arguments of methods and functions or that can be returned from methods and functions. Blocks themselves have a typed argument list and may have inferred or declared returned type. You may also assign a block to a variable and then call it just as you would a function.

And that sounds like a pretty complete description for blocks in general. No need to add to that.

But what are the advantages of blocks? One of the advantages that blocks bring into the picture is readability. You’re code will look plain & simple and easy to read because you can invoke code blocks right at the spot where you need them. Instead of defining separate functions / methods you define and run the block on the place where you need it’s output. That’s just easy.

Another advantage is the fact that you can create small reusable blocks of code and that they have actual accessibility to the local variables that are defined within the calling function’s context and all the classes variables and methods that the calling code has access too as well. That’s powerfull and enables you to create modular code with more access than the parameters that you would normally pass along (and the objects that sometimes need to be defined and sent along to pass enough information).

So, now i’ll just create blocks everywhere? No, as far as i’m concerned blocks are powerful but only a true addition if the block is compact and has a very specific goal that it needs to for-fill. If you want to notify users / the application or handle routines to sort arrays and alike then that are specific goals that need no subcomputing and complex routines. Apple gives a nice example of actions where you could use blocks to make things easier to understand:

  • Completion handlers – ie: roundup after some processing work
  • Notification handlers – ie: give the application a notification so subscribed code can jump in
  • Error handlers – handle recover or communicative actions after an error occurs
  • Enumeration – Process all the items in an array (or dictionary) using a generic block that performs the desired actions
  • View animation and transitions – Animations will be coded there where you actually perform them; very handy!
  • Sorting – sort structures (or making selections/filter elements) is a very clear example of when to use blocks.

Any catch? Although Ruby uses blocks as a backbone and blocks are one of the main ingrediënts, this isn’t the case with Objective-C. You will have to check for any classes that you use if they contain methods that you can use from within a block. If you build your own libraries or classes with functionality you can use the list above as a nice indicator for when to use blocks.

I want to read more! And so you can: the following websites will explain more about blocks for Objective-C, Ruby or in general:

Blocks in general:

Blocks in Ruby:

Blocks in Objective-C:

The wrapup is that i wanted to make you aware of the possibility to use blocks in Objective-C since I never heard about it before. Whenever i use it in code myself i’ll share my experiences with this nice feature.

Meanwhile, i’m very curious about YOUR experiences with blocks in Objective C!