Block syntax

^{kitten};
Original photo by Flickr user Cat Box2011. Creative Commons BY-NC-SA 2.0.

Blocks are an Apple extension to C and Objective-C defining functions that capture their scope. You can:

Define a block type

Template:

typedef <#return type#>(^<#type name#>)(<#parameter list#>);

Example:

typedef NSComparisonResult(^GJLStringComparator)(NSString *string1, NSString *string2);

Declare a local variable

Template:

<#return type#> (^<#variable name#>)(<#parameter list#>);

Example:

BOOL (^myFilter)(id object, NSUInteger index, BOOL *stop);

Assign a block to a variable

Template:

<#variable name#> = ^(<#parameter list#>) {
  <#code#>
  return <#return value#>;
};

Example:

myFilter = ^(id object, NSUInteger index, BOOL *stop) {
  BOOL isAwesome = [object respondsToSelector:@selector(doAwesomeThings)];
  return isAwesome;
};

Declare a block argument in an ObjC message

Template:

-(<#method return type#>)<#selector name#>:(<#block return type#>(^)(<#parameter list#>))<#block name#>;

Example:

-(NSArray *)filteredArrayWithComparator:(BOOL(^)(id object, NSUInteger index, BOOL *stop))comparator;

Pass a block to an ObjC message

Template:

[<#receiver#> <#selector name#>:^(<#parameter list#>){
  <#code#>
  return <#return value#>;
}];

Example:

[myArray filteredArrayWithComparator:^(id object, NSUInteger index, BOOL *stop) {
  BOOL isAwesome = [object respondsToSelector:@selector(doAwesomeThings)];
  return isAwesome;
}];

Note: the templates play nicely with Xcode autocomplete. Paste them directly into Xcode or add them to your snippets library.