Block编程值得注意的那些事儿

论坛 期权论坛     
选择匿名的用户   2021-5-23 01:16   5   0
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:&#39;Helvetica Neue&#39;,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:&#39;Helvetica Neue&#39;,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:&#39;Helvetica Neue&#39;,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:&#39;Helvetica Neue&#39;,Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
  block 是在 iOS 4 中引入的新特性,它和 C&#43;&#43; 11 中的 lamba 表达式概念相似,有时候也被称为闭包。经过一段时间的使用,我发现要用对用好 block 还是有不少需要注意的地方,今天就来八一八这些值得注意的事儿。
</div>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:&#39;Helvetica Neue&#39;,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:&#39;Helvetica Neue&#39;,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:&#39;Helvetica Neue&#39;,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:&#39;Helvetica Neue&#39;,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:&#39;Helvetica Neue&#39;,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:&#39;Helvetica Neue&#39;,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:&#39;Helvetica Neue&#39;,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:&#39;Helvetica Neue&#39;,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:&#39;Helvetica Neue&#39;,Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-size:14px; line-height:25px">
<pre class="blockcode"><code class="language-html">- (void)testAccessVariable
{
    NSInteger outsideVariable &#61; 10;
    //__block NSInteger outsideVariable &#61; 10;
    NSMutableArray * outsideArray &#61; [[NSMutableArray alloc] init];
     
    void (^blockObject)(void) &#61; ^(void){
        NSInteger insideVariable &#61; 20;
        KSLog(&#64;&#34;  &gt; member variable &#61; %d&#34;, self.memberVariable);
        KSLog(&#64;&#34;  &gt; outside variable &#61; %d&#34;, outsideVariable);
        KSLog(&#64;&#34;  &gt; inside variable &#61; %d&#34;, insideVariable);
         
        [outsideArray addObject:&#64;&#34;AddedInsideBlock&#34;];
    };
     
    outsideVariable &#61; 30;
    self.memberVariable &#61; 30;

    blockObject();
     
    KSLog(&#64;&#34;  &gt; %d items in outsideArray&#34;, [outsideArray count]);
}
输出结果为:

&gt; member variable &#61; 30
&gt; outside variable &#61; 10
&gt; inside variable &#61; 20
&gt; 1 items in outsideArray </code></pre>
<br>
<div style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:&#39;Helvetica Neue&#39;,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:&#39;Helvetica Neue&#39;,Hel
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP