__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都要。

转载于:https://my.oschina.net/u/2241986/blog/502712

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值