OUR BLOG

Thoughts on development, design and the world we live in.



Objective-C Gets Blocks!

By Lee in Apple, iOS, iPad, iPhone, Mobile, Objective-c. Posted on June 28th

New to Objective-C is the block syntax. A block is a section of code that is grouped together and designed to be treated like one statement. Blocks also package up the lexical scope of variables and functions within its environment. Blocks are found in many languages including, but not limited to, Ruby, Javascript and Lisp. Blocks are objects and should be treated like such!

Programming Language Code Example Colloquial
Ruby Block
Lisp Lambda function
Javascript Anonymous Function, Closure
Objective-C Block

Now there are other block structured languages out there but the few listed here are used to provide a basis for understanding if you’re coming from another language where you may have experienced this concept.

Objective-C inherited blocks from the C language, which it is a superset of. Apple has created an extension of the C language implementing blocks. Syntactically, blocks start with the carrot symbol, followed by its return type followed by arguments (enclosed by parenthesis) and finishing off with the code body defined between curly braces.

^Return Type (Arguments) {Code Body}

The blocks return type and arguments are both optional parameters, leaving them off will not effect the block.

^{Code Body}

You can pass blocks to a function:

Scoped variables (by default) are immutable in blocks. To make these mutable you prefix the keyword __block.

For example

A good example of when you might want to use a block may be a callback. Since callbacks are generally a grouping of code that will run after an operation.

One Comment

  1. Mark Levinson
    Posted April 28, 2011 at 11:34 pm | Permalink

    Thanks. The various docs on blocks do not illustrate the difference in syntax when defining a type that holds a block as opposed to the block definition itself. Turns out the syntax is different. XCode was saying “Expression Expected” if I used the same syntax for defining the block as I was using to typedef a variable to hold it. Subtle but important difference….For typedef’s, the Return Type precedes the caret (^) while it must follow the caret when defining the block itself.

    //*correct* syntax for defining a data type to hold a block
    typedef NSArray* (^my_block_type_t) (NSData *data);

    //*wrong* syntax for defining the block
    my_block_type_t blockVariable = NSArray* (^my_block_type_t) (NSData *data)
    {code body};

    //*correct* syntax for defining the block
    my_block_type_t blockVariable = ^NSArray *(NSData *data)
    {code body};

    As you explained:
    ^Return Type (Arguments) {Code Body} is the correct syntax for defining a block, while
    typedef Return Type (^my_block_type_t) (Arguments) is the correct syntax for defining a new type that stores a block.

    Thanks again.

    Thanks again.

Post a Comment

Your email is never shared. Required fields are marked *

*
*