Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>List of Block Declaration Syntaxes</h2> <p>Throughout, let</p> <ul> <li><code>return_type</code> be the type of object/primitive/etc. you'd like to return (commonly <code>void</code>)</li> <li><code>blockName</code> be the variable name of the block you're creating</li> <li><code>var_type</code> be the type object/primitive/etc. you'd like to pass as an argument (leave blank for no parameters)</li> <li><code>varName</code> be the variable name of the given parameter</li> </ul> <p>And remember that you can create as many parameters as you'd like.</p> <h3>Blocks as Variables</h3> <p>Possibly the most common for of declaration.</p> <pre><code>return_type (^blockName)(var_type) = ^return_type (var_type varName) { // ... }; </code></pre> <h3>Blocks as Properties</h3> <p>Much like declaring blocks as variables, however subtly different.</p> <pre><code>@property (copy) return_type (^blockName) (var_type); </code></pre> <h3>Blocks as Parameters</h3> <p>Note that this is distinct from "Blocks as Arguments"; in this instance, you're declaring a method that wants a block argument.</p> <pre><code>- (void)yourMethod:(return_type (^)(var_type))blockName; </code></pre> <h3>Blocks as Arguments</h3> <p>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.</p> <pre><code>[someObject doSomethingWithBlock: ^return_type (var_type varName) { //... }]; </code></pre> <h3>Anonymous Block</h3> <p>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.</p> <pre><code>^return_type (var_type varName) { //... }; </code></pre> <h3><code>typedef</code> Block</h3> <p>This allows you to set up a short name that can be referenced just like any other class name during the declaration of blocks.</p> <pre><code>typedef return_type (^blockName)(var_type); </code></pre> <p>To then later use <code>blockName</code> instead of the standard block declaration syntax, simply substitute.</p> <h3>Inline Block</h3> <p>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.</p> <pre><code>^return_type (var_type varName) { //... }(var); </code></pre> <p>Inline blocks are primarily useful for scope offsetting, and are roughly equivalent to simple brace-delimited chunks of code.</p> <pre><code>{ //... } </code></pre> <h3>Recursive Blocks</h3> <p>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.</p> <pre><code>__block return_type (^blockName)(var_type) = [^return_type (var_type varName) { if (returnCondition) { blockName = nil; return; } // ... } copy]; blockName(varValue); </code></pre> <h3>Returning Blocks</h3> <p>A method can return a block,</p> <pre><code>- (return_type(^)(var_type))methodName { // ... } </code></pre> <p>as can a function, if a bit strangely.</p> <pre><code>return_type (^FunctionName())(var_type) { // ... } </code></pre> <h2>Addendums</h2> <p>If I've missed anything, please let me know in comments, and I'll research/add them.</p> <h2>Oh, and in Swift...</h2> <pre><code>blockName = (varName: var_type) -&gt; (return_type) </code></pre> <p>It's almost like it's a language feature.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload