Blocks and Variables
This article describes the interaction between blocks and variables, including memory management.
Types of Variable
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
__block
variables. These are mutable within the block (and the enclosing scope) and are preserved if any referencing block is copied to the heap. -
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:
-
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
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.
-
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.
-
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.
The following example illustrates the use of local non-static variables:
int x = 123; |
void (^printXAndY)(int) = ^(int y) { |
printf("%d %d\n", x, y); |
}; |
printXAndY(456); // prints: 123 456 |
As noted, trying to assign a new value to x
within the block would result in an error:
int x = 123; |
void (^printXAndY)(int) = ^(int y) { |
x = x + y; // error |
printf("%d %d\n", x, y); |
}; |
To allow a variable to be changed within a block, you use the __block
storage type modifier—see The __block Storage Type.
The __block Storage Type
You can specify that an imported variable be mutable—that is, read-write— by applying the __block
storage type modifier. __block
storage is similar to, but mutually exclusive of, the register
, auto
, and static
storage types for local variables.
__block
variables live in storage that is shared between the lexical scope of the variable and all blocks and block copies declared or created within the variable’s lexical scope. Thus, the storage will survive the destruction of the stack frame if any copies of the blocks declared within the frame survive beyond the end of the frame (for example, by being enqueued somewhere for later execution). Multiple blocks in a given lexical scope can simultaneously use a shared variable.
As an optimization, block storage starts out on the stack—just like blocks themselves do. If the block is copied using Block_copy
(or in Objective-C when the block is sent a copy
), variables are copied to the heap. Thus, the address of a __block
variable can change over time.
There are two further restrictions on __block
variables: they cannot be variable length arrays, and cannot be structures that contain C99 variable-length arrays.
The following example illustrates use of a __block
variable:
__block int x = 123; // x lives in block storage |
void (^printXAndY)(int) = ^(int y) { |
x = x + y; |
printf("%d %d\n", x, y); |
}; |
printXAndY(456); // prints: 579 456 |
// x is now 579 |
The following example shows the interaction of blocks with several types of variables:
extern NSInteger CounterGlobal; |
static NSInteger CounterStatic; |
{ |
NSInteger localCounter = 42; |
__block char localCharacter; |
void (^aBlock)(void) = ^(void) { |
++CounterGlobal; |
++CounterStatic; |
CounterGlobal = localCounter; // localCounter fixed at block creation |
localCharacter = 'a'; // sets localCharacter in enclosing scope |
}; |
++localCounter; // unseen by the block |
localCharacter = 'b'; |
aBlock(); // execute the block |
// localCharacter now 'a' |
} |
相关文章
- Atitit. BigConfirmTips 控件 大数据量提示确认控件的原理and总结O9
- How to resolve .NET reference and NuGet package version conflicts
- What is the difference between visibility:hidden and display:none?
- mongodb or and 条件拼凑 Query.And Query.Or
- 【BZOJ5311/CF321E】贞鱼/Ciel and Gondolas(动态规划,凸优化,决策单调性)
- Codeforces Round #820 (Div. 3) D. Friends and the Restaurant
- Storage System and File System Courses
- 论文阅读:On-line simultaneous learning and recognition of everyday activities from virtual reality performances
- What Apache ZooKeeper is and when should it be used?
- C语言基本输入输出程序设计——计算当前时间and转换大写姓名and工资信息表
- [Python]计算闰年时候出现的and和or优先级的问题以及短路逻辑
- 4.有关日期格式属性改动常识,v$nls_parameters,between and,查询指定部门的员工信息,in和null,like模糊查询,order by后面能够跟:列名、表达式、别名、序号
- jenkins with gitblit and .net framework and windows
- 《GIoU: A Metric and A Loss for Bounding Box Regression》论文笔记