您的位置:首页 > 其它

关于block的使用

2016-04-15 09:43 113 查看
1、block的声明

[plain] view
plain copy

@interface BrushViewController : BaseViewController

@property (nonatomic, copy) void (^getCardInfo)(NSDictionary *cardInfo);

@end

用copy声明block的原因是在代码块里面可能会使用一些本地变量。而block一开始是放在栈上的,只有copy后才会放到堆上。

如果加copy属性,当其所在栈被释放的时候,这些本地变量将变得不可访问。一旦代码执行到block这段就会导致bad access。

[html] view
plain copy

brush.getCardInfo=^(NSDictionary *info){

[self test];

};

像上面这段代码,self其实是一个本地变量而不是block内部变量,如果声明为assign,代码执行到block内部就会出错。

但是这又带来另一个问题,就是self的引用计数+1。这意味着很可能会导致循环引用。self持有brush,brush持有block,block持有self。结果就是内存泄漏。

解决的办法如下:

[plain] view
plain copy

__block CurrentViewController* blockSelf = self;

brush.getCardInfo=^(NSDictionary *info){

[blockSelf test];

};





通过这个方式,告诉block这个变量的引用计数不要+1。

2、关于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强引用一次,从而不会出现循环引用问题。

API Reference对__weak变量修饰符有如下几处解释:
__weak specifies a reference that does not keep the
referenced object alive. A weak reference is set to nil when
there are no strong references to the object.


使用了__weak修饰符的对象,作用等同于定义为weak的property。自然不会导致循环引用问题,因为苹果文档已经说的很清楚,当原对象没有任何强引用的时候,弱引用指针也会被设置为nil。

因此,__block和__weak修饰符的区别其实是挺明显的:

1.__block不管是ARC还是MRC模式下都可以使用,可以修饰对象,还可以修饰基本数据类型。

2.__weak只能在ARC模式下使用,也只能修饰对象(NSString),不能修饰基本数据类型(int)。

3.__block对象可以在block中被重新赋值,__weak不可以。

PS:__unsafe_unretained修饰符可以被视为iOS SDK 4.3以前版本的__weak的替代品,不过不会被自动置空为nil。所以尽可能不要使用这个修饰符。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: