Beginning iOS 6 Development

温故而知新,重新阅读,有了新的收获,记录一下。

Note that we define a variable calledstartTimejust before the block is created, and then use
its value at the end of the block. Intuitively, this doesn’t seem to make sense, since by the time
the block is executed, the
doWork:method has exited, so theNSDateinstance that thestartTimevariable is pointing to should already be released! This is a crucial point of block usage: if a blockaccesses any variables from “the outside” during its execution, then some special setup happenswhen the block is created, allowing the block access to those variables. The values contained bysuch variables will either be duplicated (if they are plain C types such as intor float) or retained
(if they are pointers to objects) so that the values they contain can be used inside the block. When
dispatch_asyncis called in the second line ofdoWork:,and the block shown in the code is created,startTimeis actually sent aretainmessage, whose return value is assigned to what is essentially anew static variable with the same name (startTime) inside the block.

The startTimevariable needs to be static inside the block so that code inside the block can’taccidentally mess with a variable that’s defined outside the block. If that were allowed all thetime, it would just be confusing for everyone. Sometimes, however, you actually do want to let
a block write to a value defined on the outside, and that’s where the
__blockstorage qualifier(which we mentioned a couple of pages ago) comes in handy. If__blockis used to define avariable, then it is directly available to any and all blocks that are defined within the same scope.An interesting side effect of this is that__block-qualifiedvariables are not duplicated or retainedwhen used inside a block. 


记录了在block 里面,不同的变量,不同的内存管理方式。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值