I know that an Objective-C Block can capture and set the value of variables outside of its enclosing scope. How does it do that?
Within the block object’s body of code, variables may be treated in five different ways.
You can reference three standard types of variable, just as you would from a function:
- Global variables, including static locals
- Global functions (which aren’t technically variables)
- Local variables and parameters from an enclosing scope
Blocks also support two other types of variable:
At function level are
__blockvariables. These are mutable within the block (and the enclosing scope) and are preserved if any referencing block is copied to the heap.
constimports.Finally, within a method implementation, blocks may reference Objective-C instance variables—see Object and Block Variables.
The following rules apply to variables used within a block:
Global variables are accessible, including static variables that exist within the enclosing lexical scope.
Parameters passed to the block are accessible (just like parameters to a function).
Stack (non-static) variables local to the enclosing lexical scope are captured as
constvariables.Their values are taken at the point of the block expression within the program. In nested blocks, the value is captured from the nearest enclosing scope.
Variables local to the enclosing lexical scope declared with the
__blockstorage modifier are provided by reference and so are mutable.Any changes are reflected in the enclosing lexical scope, including any other blocks defined within the same enclosing lexical scope. These are discussed in more detail in The __block Storage Type.
Local variables declared within the lexical scope of the block, which behave exactly like local variables in a function.
Each invocation of the block provides a new copy of that variable. These variables can in turn be used as
constor by-reference variables in blocks enclosed within the block.
From here:
http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/Blocks/Articles/bxVariables.html