码迷,mamicode.com
首页 > 其他好文 > 详细

__block在什么情况下使用

时间:2015-09-08 12:44:34      阅读:183      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!