1.IOS中 __weak 和 __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.
大致的意思是:(1) __block 修饰的对象在block 内部进行进行使用时.不要把修饰的对象当做常量,而是当做一个变量来使用.
(2) __block修饰的对象可以防止出现循环引用.
// __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,所以可以解决block的循环引用, 并且_weak 修饰的对象的引用计数为0的时候, 修饰的对象为nil
说了这么多 _weak和_block的区别:
1.__block不但可以在ARC环境下面使用还可以在MRC环境下面使用,并且可以修饰对象和基本数据类型.
2.__weak 只能在ARC下使用,并且只能修饰对象,不能修饰基本数据类型.
3._block修饰的对象在block 内部可以被修改,而__weak 修饰的对象不能被修改