Skip to content

Instantly share code, notes, and snippets.

@tnhu
Created March 20, 2015 20:00
Show Gist options
  • Select an option

  • Save tnhu/253ab8693e6caab68feb to your computer and use it in GitHub Desktop.

Select an option

Save tnhu/253ab8693e6caab68feb to your computer and use it in GitHub Desktop.
Declare A Block in Objective-C

How Do I Declare A Block in Objective-C?

Source: http://fuckingblocksyntax.com

As a local variable:

returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};

As a property:

@property (nonatomic, copy) returnType (^blockName)(parameterTypes);

As a method parameter:

- (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;

As an argument to a method call:

[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];

As a typedef:

typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment