A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wslinke 中级黑马   /  2015-4-18 20:36  /  606 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 wslinke 于 2015-4-18 20:38 编辑

摘自苹果官方文档 自己翻译的,英文水平不好,请见谅
Blocks Can Capture Values from the Enclosing Scope
As well as containing executable code, a block also has the ability to capture state from its enclosing scope.
就像包含可执行代码一样,block同样有能力从封闭范围内获取状态
If you declare a block literal from within a method, for example, it’s possible to capture any of the values accessible within the scope of that method, like this:
例如,如果你在一个方法内声明了一个block, 它将可以获取在这个方法范围内任何可以访问的值,就像这样

  1. - (void)testMethod {
  2.     int anInteger = 42;
  3.     void (^testBlock)(void) = ^{
  4.         NSLog(@"Integer is: %i", anInteger);
  5. };
  6.     testBlock();
  7. }
复制代码


In this example, anInteger is declared outside of the block, but the value is captured when the block is defined.
在这个例子中,anInteger被定义在了block的外面,但在block被定义时,它的值被获取到了
Only the value is captured, unless you specify otherwise. This means that if you change the external value of the variable between the time you define the block and the time it’s invoked, like this:



只有值被获取到了,除非你额外指定(否则值是不会改变的).这意味着如果你在调用block之前,且在声明这个block之后改变了一个外部变量(这个变量的值是不会被重新赋给block的),像这样:

  1. int anInteger = 42;
  2. void (^testBlock)(void) = ^{
  3.     NSLog(@"Integer is: %i", anInteger);
  4. };
  5. anInteger = 84;
  6. testBlock();
复制代码


the value captured by the block is unaffected. This means that the log output would still show:
block所获取的值并没有被影响到,这意味着输出仍然会是:
  1. Integer is: 42
复制代码


It also means that the block cannot change the value of the original variable, or even the captured value (it’s captured as a const variable).
这同样意味着block不能改变源变量的值,甚至不能改变获取到的值(它将被获取视为一个const变量)
Use __block Variables to Share Storage
If you need to be able to change the value of a captured variable from within a block, you can use the __block storage type modifier on the original variable declaration.
如果你想要在block里改变一个获取到的变量的值,你可以在源变量声明时使用__block这种存储类型修饰符来修饰它.
This means that the variable lives in storage that is shared between the lexical scope of the original variable and any blocks declared within that scope.
这意味着该变量可以在任意一个处于该变量作用域的block中共享数据
As an example, you might rewrite the previous example like this:
例如,你可以像这样重写上面那个例子:
  1. Integer is: 42
  2. __block int anInteger = 42;
  3. void (^testBlock)(void) = ^{
  4.     NSLog(@"Integer is: %i", anInteger);
  5. };
  6. anInteger = 84;
  7. testBlock();
复制代码


Because anInteger is declared as a __block variable, its storage is shared with the block declaration. This means that the log output would now show:
由于anInteger 被声明为__block变量,它将被存储为与block声明共享的变量,这意味着输出将会变为:
  1. Integer is: 84
复制代码


It also means that the block can modify the original value, like this:
这同样意味着block可以修改原始数值,像这样:
  1. __block int anInteger = 42;
  2. void (^testBlock)(void) = ^{
  3.     NSLog(@"Integer is: %i", anInteger);
  4.     anInteger = 100;
  5. };
  6. testBlock();
  7. NSLog(@"Value of original variable is now: %i", anInteger);
复制代码


This time, the output would show:
这次,输出将会是:
  1. Integer is: 42
  2. Value of original variable is now: 100
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马