Skip to content

Instantly share code, notes, and snippets.

@lukas2
Forked from swizzlr/blockdeclsyntax.md
Created June 6, 2019 09:14
Show Gist options
  • Save lukas2/456920a2bfb00018dbcaea614e0efa74 to your computer and use it in GitHub Desktop.
Save lukas2/456920a2bfb00018dbcaea614e0efa74 to your computer and use it in GitHub Desktop.
Block Declaration Syntax List, originally by [pcperini](http://stackoverflow.com/a/9201774/929581), parsed to markdown for printing with your favourite CSS (I recommend @ttscoff's Marked)

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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment