How does a Block capture the variables outside of its enclosing scope?

前端 未结 3 783
再見小時候
再見小時候 2020-12-01 09:45

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?

3条回答
  •  眼角桃花
    2020-12-01 10:18

    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:

    1. At function level are __block variables. These are mutable within the block (and the enclosing scope) and are preserved if any referencing block is copied to the heap.

    2. const imports.

    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:

    1. Global variables are accessible, including static variables that exist within the enclosing lexical scope.

    2. Parameters passed to the block are accessible (just like parameters to a function).

    3. Stack (non-static) variables local to the enclosing lexical scope are captured as const variables.

      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.

    4. Variables local to the enclosing lexical scope declared with the __block storage 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.

    5. 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 const or by-reference variables in blocks enclosed within the block.

    From here:
    http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/Blocks/Articles/bxVariables.html

提交回复
热议问题