Objective-C Blocks Cheat Sheet

Posted by on Dec 6, 2012 in iOS, iPad, iPhone, Mac OS X | 6 Comments

iOS Blocks Objective-CBlocks are an incredibly powerful addition to Objective-C, introduced in iOS 4. However, their syntax can be maddeningly difficult to remember. Matt Gallagher has an excellent post that breaks down the syntax to help you understand it. If you haven’t read this article, go do it now.

Even after working with blocks for a while, I still get tripped up. So I created the cheat sheet below, which I frequently refer to when declaring blocks.

// http://cocoawithlove.com/2009/10/ugly-side-of-blocks-explicit.html has a nice breakdown of the syntax--it helps to think of the ^ as similar to a pointer dereference symbol *

// block typedef:

    typedef void(^Block)();
    typedef void(^ConditionalBlock)(BOOL);
    typedef NSString*(^BlockThatReturnsString)();
    typedef NSString*(^ConditionalBlockThatReturnsString)(BOOL);

// block property with typedef:

    @property(copy)Block theBlock;

// block property without typedef:

    @property(copy)void(^block)();
    @property(copy)void(^conditionalBlock)(BOOL);
    @property(copy)NSString*(^blockThatReturnsString)();
    @property(copy)NSString*(^conditionalBlockThatReturnsString)(BOOL);


// block definition inline:

    ReturnType(^block_name)(parmeter, types, here) = ^(parameter, types, here) {};
    
    void(^block)(void) = ^{};    
    void(^conditionalBlock)(BOOL shouldWork) = ^(BOOL shouldWork){};
    NSString*(^blockThatReturnsString)(void) = ^ NSString* { return @"foo"; };    
    NSString*(^conditionalBlockThatReturnsString)(BOOL shouldWork) = ^ NSString* (BOOL shouldWork){ 
        return shouldWork ? @"foo" : @"bar"; 
    };

    -(void)runBlocks {
        block();
        conditionalBlock(NO);
        NSString *someString = blockThatReturnsString();
        NSString *conditionalString = conditionalBlockThatReturnsString(NO);
    }

// blocks as return values:

    -(void (^)(void))doSomething;
    -(void (^)(BOOL))doSomethingConditionally;
    -(NSString* (^)(void))returnString;
    -(NSString* (^)(BOOL))returnStringConditionally;

    -(NSString* (^)(BOOL))returnStringConditionally {
        return ^(BOOL shouldReturn) {
            if (shouldReturn) return @"foo";
            return @"bar";
        }
    }
    
// blocks as arguments:

    -(void)doSomethingWithBlock:(void (^)(void))block;
    -(void)doSomethingWithBlock:(void (^)(BOOL))conditionalBlock;
    -(void)doSomethingWithBlock:(NSString* (^)(void))blockThatReturnsString;
    -(void)doSomethingWithBlock:(NSString* (^)(BOOL))conditionalBlockThatReturnsString;

6 Comments

  1. Cheat sheet for Objective-C blocks « [iOS developer:tips];
    September 27, 2013

    […] And with that, Christopher created a blocks cheatsheet. […]

    Reply
  2. Objective-C Blocks Cheat Sheet | zdima.net
    September 28, 2013

    […] to help you understand it. If you haven’t read this article, go do it now. via Feedly/Pocket twobitlabs.com/2012/12/objective-c-ios-blocks-cheat-sheet/ on September 28, 2013 at […]

    Reply
  3. Christopher Pickslay
    October 8, 2013

    Another nice resource: Fucking Block Syntax

    Reply
  4. Joseph
    September 9, 2014

    Its good. Thank you.

    Reply
  5. Ken
    April 6, 2015

    You are my hero.

    Reply
  6. Julian Raschke
    October 30, 2015

    Nitpick: Your examples use void (^block)() in some places and void (^block)(void) in others. In Objective-C, you should always use (void) for blocks without parameters – an empty argument list actually declares a variable-argument block.
    In Objective-C++, () denotes an empty argument list as it should.

    Reply

Leave a Reply