weak修饰的对象在释放之后,指针地址会被置为nil。所以现在一般弱引用就是用weak。weak使用场景:
在ARC下,在有可能出现循环引用的时候,往往要通过让其中一端使用weak来解决,比如: delegate代理属性,通常就会声明为weak。自身已经对它进行一次强引用,没有必要再强引用一次时也会使用weak。比如:自定义 IBOutlet控件属性一般也使用weak,当然也可以使用strong。__block是用来修饰一个变量,这个变量就可以在block中被修改
__block:使用 __block修饰的变量在block代码块中会被retain(ARC下会retain,MRC下不会retain)
__weak:使用__weak修饰的变量不会在block代码块中被retain 同时,在ARC下,要避免block出现循环引用 __weak typedof(self)weakSelf = self;特别需要注意的地方就是在把block放到集合类当中去的时候,如果直接把生成的block放入到集合类中,是无法在其他地方使用block,必须要对block进行copy。
[array addObject:[[^{ NSLog(@"hello!"); } copy] autorelease]];官方文档:You should specify copy as the property attribute, because a block needs to be copied to keep track of its captured state outside of the original scope. This isn’t something you need to worry about when using Automatic Reference Counting, as it will happen automatically, but it’s best practice for the property attribute to show the resultant behavior
希望能对大家有所帮助!!!