天天看點

[翻譯] Blocks and Variables

blocks and variables

[翻譯] Blocks and Variables

<a href="https://developer.apple.com/library/ios/documentation/cocoa/conceptual/blocks/articles/bxvariables.html" target="_blank">https://developer.apple.com/library/ios/documentation/cocoa/conceptual/blocks/articles/bxvariables.html</a>

<a href="http://stackoverflow.com/questions/16149653/what-is-the-role-of-the-copy-in-the-arc" target="_blank">http://stackoverflow.com/questions/16149653/what-is-the-role-of-the-copy-in-the-arc</a>

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

這篇文章描述了 blocks 與變量間的互動作用,同時也包括記憶體管理。

types of variable

within the block object’s body of code, variables may be treated in five different ways.

在 block 對象體中插入的代碼,變量可以分為5種。

you can reference three standard types of variable, just as you would from a function:

你可以引用 3 種标準類型的變量,就像你在普通方法中使用的那樣子:

global variables, including static locals  全局變量,包括 static 修飾過的靜态變量

global functions (which aren’t technically variables) 全局方法(技術上來說不能被稱作變量)

local variables and parameters from an enclosing scope 局部變量和從上下文中帶進來的參數

blocks also support two other types of variable:

blocks 也支援另外兩種類型的變量:

at function level are <code>__block</code> variables. these are mutable within the block (and the enclosing scope) and are preserved if any referencing block is copied to the heap. 函數級别上的 __block 修飾的對象。它在block裡面是可以修改的,如果這個 block 被 copy 到了棧區,這個對象就會被強引用。

<code>const</code> imports. const引入的。

the following rules apply to variables used within a block:

以下規則适用于在 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). 傳遞到 block 中的變量(就像函數傳遞參數一樣)

stack (non-static) variables local to the enclosing lexical scope are captured as <code>const</code> variables. 相對于 block 塊的非靜态堆區對象被識别為 const 對象。

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.  他們的值會以指針的形式傳遞到 block 中。

variables local to the enclosing lexical scope declared with the <code>__block</code> storage modifier are provided by reference and so are mutable.  __block 修飾的對象允許在 block 中進行修改,并會被 block 強引用。

local variables declared within the lexical scope of the block, which behave exactly like local variables in a function. 在 block 塊中執行個體化的對象,與在函數中執行個體化的對象基本一緻。

each invocation of the block provides a new copy of that variable. these variables can in turn be used as <code>const</code> or by-reference variables in blocks enclosed within the block. 每一次調用這個 block 都會提供一個變量的 copy。相應的,這些對象可以被當做 const 或者是強引用的對象使用。

the following example illustrates the use of local non-static variables:

以下例子描述了如何使用一個本地非 static 的變量:

as noted, trying to assign a new value to <code>x</code> within the block would result in an error:

正如提到的那樣,給 x 在 block 中直接指派會引發錯誤:

the __block storage type

you can specify that an imported variable be mutable—that is, read-write— by applying the <code>__block</code> storage type modifier. <code>__block</code> storage is similar to, but mutually exclusive of, the <code>register</code>, <code>auto</code>, and <code>static</code> storage types for local variables.

你可以指定引入的對象可以被修改,那就是,可讀可寫。通過給這個變量修飾 __block 存儲修改類型。__block 存儲與 register ,auto,static 存儲方式互斥(對于一個别修飾的變量)。

<code>__block</code> 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.

__block 變量在一個容器中存活,可以被變量的上下文共享,可以被所有 block 共享,可以被 copy 修飾過的 block 共享,以及在 block 塊中建立的對象共享。也就是說,如果有任何 copy 出來的 block 用了這個變量,它會一直存活于堆區當中。

as an optimization, block storage starts out on the stack—just like blocks themselves do. if the block is copied using <code>block_copy</code> (or in objective-c when the block is sent a <code>copy</code>), variables are copied to the heap. thus, the address of a <code>__block</code> variable can change over time.

作為一個優化,block 存儲開始與堆區,就像 blocks 他們自己做的那樣子。如果這個 block 被 copy 了(或者在 oc 當中 block 接收到了 copy 消息)。變量就會被複制到棧區去。也就是說,這個變量可以一直被修改了。

there are two further restrictions on <code>__block</code> variables: they cannot be variable length arrays, and cannot be structures that contain c99 variable-length arrays.

對于 __block 變量有着兩點限制:他們不能用于可變長度的數組,也不能包括c99中可變長度數組的結構體。

the following example illustrates use of a <code>__block</code> variable:

以下例子描述了怎麼使用 __block 變量:

the following example shows the interaction of blocks with several types of variables:

以下例子顯示了 blocks 如何與不同類型的變量互動:

繼續閱讀