__block在什么状况下使用

如下为转发spa

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

//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都要。
ci

相关文章
相关标签/搜索