首页 > 其他 > 详细

__block在什么情况下使用

时间:2015-09-08 12:42:38      阅读:283      评论:0      收藏:0      [点我收藏+]

以下为转发

API Reference对__block变量修饰符有如下几处解释:

//A powerful feature of blocks is that they can modify 
variables in the same lexical scope. You signal that a block 
can modify a variable using the __block storage type 
modifier. 
//At function level are __block variables. These are mutable
 within the block (and the enclosing scope) and are preserved if any referencing block is copied to the heap.

大概意思归结出来就是两点:
1.__block对象在block中是可以被修改、重新赋值的。
2.__block对象在block中不会被block强引用一次,从而不会出现循环引用问题。

对于第2点,主要用在非ARC环境下,解决循环引用的问题,在ARC环境下要用weak来解决循环引用的问题

对于第1点,是指在block中,如果只是用外部的局部变量的值,那不需要修饰;如果需要修改外部的局部变量的值,就需要__block修饰,ARC和非ARC都要。

__block在什么情况下使用

原文:http://my.oschina.net/u/2241986/blog/502712

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!