天天看点

__weak 和 __block 关键字的区别

问题描述:在使用block 时,为避免亲戚循环引用问题,我们一般常将外部变量用 __weak 或者 __block 关键字进行修饰。那么二者的区别在哪呢?

下面引自官方的说明:

From the docs about __block

__block variables live in storage that is shared between the lexical scope of the variable and all blocks and block copies declared or created w

So they are technically different things. __block is to stop your variable being copied from your external scope into your block scope. __weak is a self delimiting weak pointer.

From the docs about __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.

So they are technically different things. __block is to stop your variable being copied from your external scope into your block scope. __weak is a self delimiting weak pointer.

Note I said technically, because for your case they will do (almost) the same thing. The only difference is if you are using ARC or not. If your project uses ARC and is only for iOS4.3 and above, use __weak. It ensures the reference is set to nil if the global scope reference is releases somehow. If your project doesn't use ARC or is for older OS versions, use __block.

There is a subtle difference here, make sure you understand it.

EDIT: Another piece to the puzzle is __unsafe_unretained. This modifier is almost the same as __weak but for pre 4.3 runtime environments. HOWEVER, it is not set to nil and can leave you with hanging pointers.

AIn manual reference counting mode, __block id x; has the effect of not retaining x. In ARC mode, __block id x; defaults to retaining x (just like all other values). To get the manual reference counting mode behavior under ARC, you could use __unsafe_unretained __block id x;. As the name __unsafe_unretained implies, however, having a non-retained variable is dangerous (because it can dangle) and is therefore discouraged. Two better options are to either use __weak (if you don’t need to support iOS 4 or OS X v10.6), or set the __block value to nil to break the retain cycle.

自我总结一下就是:

1,在MRC 时代,__block 修饰,可以避免循环引用;ARC时代,__block 修饰,同样会引起循环引用问题;

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

3,__weak只能在ARC模式下使用,也只能修饰对象,不能修饰基本数据类型;

4,__block对象可以在block中被重新赋值,__weak不可以;

5,__unsafe_unretained修饰符可以被视为iOS SDK 4.3以前版本的__weak的替代品,不过不会被自动置空为nil。所以尽可能不要使用这个修饰符。(__weak 会自动置为nil)