您的位置:首页 > 其它

__block在什么情况下使用

2015-09-08 00:00 267 查看
摘要: 总结__block修饰的作用

以下为转发

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都要。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: