User Tools

Site Tools


ios-labs-s14:advanced-blocks

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
ios-labs-s14:advanced-blocks [2014/03/01 11:38]
mbarboi created
ios-labs-s14:advanced-blocks [2015/10/30 11:54]
mbarboi
Line 1: Line 1:
 =====Blocks===== =====Blocks=====
-A block is a chunk of code encapsulated within a variable. It can be passed along as a method parameter or assigned as a class property. It can be executed at any point. Blocks can be passed arguments. ​+A block (or closure) ​is a chunk of code encapsulated within a variable. It can be passed along as a method parameter or assigned as a class property. It can be executed at any point. Blocks can be passed arguments. ​
  
 The main usefulness of blocks is the ability to provide reactive implementations to events that may occur asynchronously or in remote objects. Examples: handling asynchronous HTTP replies, reacting to the end of an animation, etc. The main usefulness of blocks is the ability to provide reactive implementations to events that may occur asynchronously or in remote objects. Examples: handling asynchronous HTTP replies, reacting to the end of an animation, etc.
  
-In other languages ​blocks ​are also known as closures. ​+With Swift, the name of blocks ​changed to "closures."
  
 +See [[http://​fuckingswiftblocksyntax.com/​|freakingswiftblocksyntax.com]] for swift closure syntax. Ignore the section below. ​
  
-===Using Blocks===+===Using Blocks ​(Deprecated:​ Objective-C)===
 Blocks are always denoted by a caret (^). This section will only cover the use of blocks provided by other classes, not the creation of your own blocks. Blocks are always denoted by a caret (^). This section will only cover the use of blocks provided by other classes, not the creation of your own blocks.
  
Line 33: Line 34:
 </​code>​ </​code>​
  
 +Note the curly braces wrapping the code, followed the the square bracket closing the method call. 
  
 +Blocks introduce a number of scope issues:
 +  -Local and instance variables cannot be referred to in a block unless declared with the "​__block"​ prefix
 +  -Properties can be used using the "​self.property"​
 +  -"​self"​ can be used in a block, but doing so **captures self**. This is potentially dangerous
 +
 +Best practice is **not** to access local, instance, or public variables of a class within a block literal defined in that class.
ios-labs-s14/advanced-blocks.txt ยท Last modified: 2015/10/30 11:54 by mbarboi