281

Block syntax in Objective C (and indeed C, I presume) is notoriously incongruous. Passing blocks as arguments looks different than declaring blocks as ivars, which looks different than typedefing blocks.

Is there a comprehensive list of block-declaration syntax that I could keep on hand for quick reference?

Patrick Perini
  • 22,555
  • 12
  • 59
  • 88
  • 9
    What's wrong with ["Declaring and Creating Blocks"](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Blocks/Articles/bxDeclaringCreating.html%23//apple_ref/doc/uid/TP40007502-CH4-SW1) in the Blocks Programming Topics Guide? – jscs Feb 08 '12 at 21:27
  • straightforward explanation of using block properties: http://stackoverflow.com/a/20760583/294884 – Fattie Apr 25 '15 at 08:27

7 Answers7

700

List of Block Declaration Syntaxes

Throughout, let

  • return_type be the type of object/primitive/etc. you'd like to return (commonly void)
  • blockName be the variable name of the block you're creating
  • var_type be the type object/primitive/etc. you'd like to pass as an argument (leave blank for no parameters)
  • varName be the variable name of the given parameter

And remember that you can create as many parameters as you'd like.

Blocks as Variables

Possibly the most common for of declaration.

return_type (^blockName)(var_type) = ^return_type (var_type varName)
{
    // ...
};

Blocks as Properties

Much like declaring blocks as variables, however subtly different.

@property (copy) return_type (^blockName) (var_type);

Blocks as Parameters

Note that this is distinct from "Blocks as Arguments"; in this instance, you're declaring a method that wants a block argument.

- (void)yourMethod:(return_type (^)(var_type))blockName;

Blocks as Arguments

Note that this is distinct from "Blocks as Parameters"; in this instance, you're calling a method that wants a block argument with an anonymous block. If you have already declared a block variable, it is sufficient to pass the variable name as the argument.

[someObject doSomethingWithBlock: ^return_type (var_type varName)
{
    //...
}];

Anonymous Block

This is functionally an anonymous block, however the syntax for assigning blocks to variables is simply to set the variable equal to an anonymous block.

^return_type (var_type varName)
{
    //...
};

typedef Block

This allows you to set up a short name that can be referenced just like any other class name during the declaration of blocks.

typedef return_type (^blockName)(var_type);

To then later use blockName instead of the standard block declaration syntax, simply substitute.

Inline Block

This is arguably a less useful utilization of blocks, but may have its place nonetheless. An inline block is an anonymous block called immediately after instantiation.

^return_type (var_type varName)
{
    //...
}(var);

Inline blocks are primarily useful for scope offsetting, and are roughly equivalent to simple brace-delimited chunks of code.

{
   //...
}

Recursive Blocks

This allows you to call a block from itself, creating a loop that can be used during callbacks and GCD calls. This instantiation method is free of retain cycles in ARC.

__block return_type (^blockName)(var_type) = [^return_type (var_type varName)
{
    if (returnCondition)
    {
        blockName = nil;
        return;
    }

    // ...
} copy];
blockName(varValue);

Returning Blocks

A method can return a block,

- (return_type(^)(var_type))methodName
{
    // ...
}

as can a function, if a bit strangely.

return_type (^FunctionName())(var_type)
{
    // ...
}

Addendums

If I've missed anything, please let me know in comments, and I'll research/add them.

Oh, and in Swift...

blockName = (varName: var_type) -> (return_type)

It's almost like it's a language feature.

Patrick Perini
  • 22,555
  • 12
  • 59
  • 88
  • 1
    @pcperini A great list! And how to use typedef'd block definition in variable declaration (1st item in your list)? Logical ```BlockType ^blockVar = Anonymous Block``` shows syntax error, without ^ too :( – esp May 20 '13 at 14:49
  • 14
    Somehow my brain is incapable of remembering all these various syntax for block declarations. I probably look at this answer once a week. Wish I could give you 10 upvotes for this. – Ben Baron May 28 '13 at 01:12
  • 37
    We need a StackOverflow Hall of Fame for answers like this. – Jonathan Beebe Jul 25 '13 at 22:26
  • 1
    I've converted this into Markdown for use with a viewer for printing. Handy! https://gist.github.com/swizzlr/6268955 – Swizzlr Aug 19 '13 at 13:13
  • Blocks as return types (from objective-c methods, from C functions, and from other blocks) are missing from this. – Mark Amery Sep 28 '13 at 16:44
  • @MarkAmery I cannot seem to nail the syntax for returning a block from another block without a `typedef`. Any help you could lend here would be phenomenal! – Patrick Perini Sep 30 '13 at 16:08
  • @MarkAmery So funny... I did the same thing.. and THEN noticed you had doe it already! D'oh! https://gist.github.com/6858475 – Alex Gray Oct 06 '13 at 20:44
  • 22
    I think I need this as a tattoo. – Isaac Overacker Nov 21 '13 at 00:21
  • 1
    If you use this often, like Ben (einsteinx2), I would suggest copying it to a text file somewhere and enclosing the variables in "<#" and "#>" so that Xcode treats them as placeholders when you copy and paste them. --Rob in Pittsburgh – Rob at TVSeries.com Dec 15 '13 at 13:26
  • Hi pcperini! In your fine example **"Blocks as Parameters"**. Say I then have a method +(void)blah:(return_type(^)(var_type))after ... how do I then set self.blockName = after ... ? It doesn't seem to work. This is in a normal modern ARC setting. THANKS! – Fattie Dec 23 '13 at 15:11
  • @JoeBlow what doesn't work about it? Compiler error? Runtime error? Gonna need a bit more info :) – Patrick Perini Dec 24 '13 at 06:13
  • hi PC! You know, I did get it working. But, I suggest you include an example of that in your section "Blocks as Parameters"! For your millions of fans on this answer! :) FOr anyone struggling with this, I put a full example here .. http://stackoverflow.com/questions/3935574/can-i-use-objective-c-blocks-as-properties/20760583#20760583 Because this has **changed a lot over the years in the Xcode milieu** there is a lot of out-of-date information on it. Thank goodness for your answer here, pcperini. Merry Christmas 2013. – Fattie Dec 24 '13 at 12:03
  • I found myself using inline block syntax, so I thought I'd contribute one scenario where it's useful: reducing redundancy in switch statements. Instead of `switch (value) { case 1: _someTextField.stringValue = @"one"; break; case 2: ... }`, I have `_someTextField.stringValue = ^NSString *(){ switch (value) { case 1: return @"one"; case 2: ... } }();`. Each case line gets a lot shorter, and if I want to change what I'm doing with the different values (e.g. using attributed strings instead), I only have to do it in one place. – Siobhán May 21 '14 at 07:10
  • 1
    This is pure GOLD! – SudoPlz Jun 21 '18 at 19:51
86

I personally like using this website (http://fuckingblocksyntax.com). The name is easier to remember than the block syntax itself:

http://fuckingblocksyntax.com

and if you can't load URLs with bad words in them you can use this mirror: http://goshdarnblocksyntax.com

fuckingblocksyntax website

psy
  • 2,791
  • 26
  • 26
41

Typedef:

typedef void (^block)(NSString *arg);

Inline:

void (^block)(NSString *) = ^(NSString *param) {
  // do something....
};

Method:

- (void)method:(void (^)(NSString *param))handler
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • This answer shows that it's not that complicated after all... just 3 different syntaxes to mix and match. – Joseph Chen Feb 07 '14 at 01:57
  • 4
    The accepted answer is just a copy of this answer with unnecessary bloat. –  Feb 07 '14 at 18:18
16

The Xcode 4 snippet library contains templates for block typedefs and inline blocks as variables. They are also available via auto-completion (typedefblock and inlineblock).

For blocks as arguments to methods, I'd recommend declaring a typedef and then simply using that. It makes the code much easier to read.

omz
  • 53,243
  • 5
  • 129
  • 141
12

I wrote a completionBlock for a class which will return the values of a dice after they have been shaked:

  1. Define typedef with returnType (.h above @interface declaration)

    typedef void (^CompleteDiceRolling)(NSInteger diceValue);
    
  2. Define a @property for the block (.h)

    @property (copy, nonatomic) CompleteDiceRolling completeDiceRolling;
    
  3. Define a method with finishBlock (.h)

    - (void)getDiceValueAfterSpin:(void (^)(NSInteger diceValue))finishBlock;
    
  4. Insert previous defined method in .m file and commit finishBlock to @property defined before

    - (void)getDiceValueAfterSpin:(void (^)(NSInteger diceValue))finishBlock{
        self.completeDiceRolling = finishBlock;
    }
    
  5. To trigger completionBlock pass predefined variableType to it (Don't forget to check whether the completionBlock exists)

    if( self.completeDiceRolling ){
        self.completeDiceRolling(self.dieValue);
    }
    
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
7
typedef void (^OkBtnBlock)(id data);
typedef void (^CancelBtnBlock)();

@property (nonatomic, strong) OkBtnBlock okBtnBlock;
@property (nonatomic, strong) CancelBtnBlock cancelBtnBlock;

+ (void)foo:(OkBtnBlock)okBtn andCancel:(CancelBtnBlock)btnCancel;
benhi
  • 574
  • 1
  • 6
  • 24
  • Would be good to describe the whole process step by step, blocks are hard to understand if you are new to ios... – Alex Cio May 16 '19 at 17:52
3

If you need to work back in Xcode 4.2, you can also @synthesize a block declared as a property just like you would with a non block property. Don't let the block syntax throw you.

If your block property is this:

@property (copy) return_type (^blockName) (var_type);

Then your @synthesize is this:

@property blockName;

Cheers.

Alex Zavatone
  • 4,106
  • 36
  • 54
  • Hi, could you please recheck.. I was just trying to follow you.. `@property blockName` is not working. I think it should be `@synthesize blockName;` ? (for synthesizing a block) – jeet.chanchawat Jan 16 '16 at 02:48
  • oops... wait you already (indirectly) mentioned that it wont work with xcode 7. – jeet.chanchawat Jan 16 '16 at 02:53