IOS block编程指南 5 Block和变量

Blocks and Variables(Block和变量)

This article describes the interaction between blocks and variables, including memory management.

本文讲述了block和变量之间的内在关系,包括内存管理。

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

block对象中的代码中,变量会被五种方法区别对待。

你可以参考三种标准变量,就像你在一个函数中那样:

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—seeObject and Block Variables.

block也支持其他两种类型的变量

在函数级别的_block 变量。这些变量在block中是可变的,同时如果引用变量的block拷贝进堆堆,手动管理内存)内存就会保存 。常量

最后,随着方法的实现,blocks会引用objective-C的实例变量——参见: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 asconstvariables.

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 inThe __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 asconstor by-reference variables in blocks enclosed within the block.

以下规则适用于block中使用的变量:

作用域中的_block变量提供的是引用,所以是可变的。 所有的变动都会反映在作用域中,包括同一个作用域内定义了其他的block。更具体的讨论:见The __block Storage Type。block中声明的局部变量,他们的行为很像函数中的局部变量。 block的每次调用都会给变量提供一份新的拷贝。这些在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 toxwithin the block would result in an error:

值得注意的是,试图在块内给block赋值会返回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__blockstorage type modifier—seeThe __block Storage Type.

为了允许在block中修改变量,你需要用 _block 类型存储 ,见:The __block Storage Type。

The __block Storage Type(_block 存储类型)

You can specify that an imported variable be mutable—that is, read-write— by applying the__blockstorage type modifier.__blockstorage is similar to, but mutually exclusive of, theregister,auto, andstaticstorage types for local variables.

__blockvariables 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.

每天告诉自己一次,我真的很不错

IOS block编程指南 5 Block和变量

相关文章:

你感兴趣的文章:

标签云: