<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
<strong>转自:http://www.cnblogs.com/kesalin/archive/2013/04/30/ios_block.html</strong>
</div>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
<strong>一,前言</strong>
</div>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
</div>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
block 是在 iOS 4 中引入的新特性,它和 C++ 11 中的 lamba 表达式概念相似,有时候也被称为闭包。经过一段时间的使用,我发现要用对用好 block 还是有不少需要注意的地方,今天就来八一八这些值得注意的事儿。
</div>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
</div>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
<strong> 本文源码下载:</strong>
<a href="http://files.cnblogs.com/kesalin/KSTestApp.zip" style="font-size:12px; line-height:normal; text-decoration:none; color:rgb(102,102,102)"><span style="color:rgb(0,0,255)"><strong>点此下载</strong></span></a>
</div>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
</div>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
<strong>二,block 注意事项</strong>
</div>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
</div>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
<strong>1,block 在实现时就会对它引用到的它所在方法中定义的栈变量进行一次只读拷贝,然后在 block 块内使用该只读拷贝。</strong>
</div>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
</div>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
如下代码:
</div>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
<pre class="blockcode"><code class="language-html">- (void)testAccessVariable
{
NSInteger outsideVariable = 10;
//__block NSInteger outsideVariable = 10;
NSMutableArray * outsideArray = [[NSMutableArray alloc] init];
void (^blockObject)(void) = ^(void){
NSInteger insideVariable = 20;
KSLog(@" > member variable = %d", self.memberVariable);
KSLog(@" > outside variable = %d", outsideVariable);
KSLog(@" > inside variable = %d", insideVariable);
[outsideArray addObject:@"AddedInsideBlock"];
};
outsideVariable = 30;
self.memberVariable = 30;
blockObject();
KSLog(@" > %d items in outsideArray", [outsideArray count]);
}
输出结果为:
> member variable = 30
> outside variable = 10
> inside variable = 20
> 1 items in outsideArray </code></pre>
<br>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
注意到没?outside 变量的输出值为10,虽然outside变量在定义 block 之后在定义 block 所在的方法 testAccessVariable 中被修改为 20 了。这里的规则就是:blockObject 在实现时会对 outside 变量进行只读拷贝,在 block 块内使用该只读拷贝。因此这里输出的是拷贝时的变量值 10。如果,我们想要让 blockObject 修改或同步使用 outside 变量就需要用 __block 来修饰 outside 变量。
</div>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Hel |
|