最近都在折腾Sagit架框的内存释放的问题,所以对这一块有些心得。
对于新手,学的到的文章都在教你用:<代码> typeof(自我)__weak weakSelf=自我代码>。
对于老的手,可能早习惯了到处了WeakSelf了。
这次,就来学,学如何不用WeakSelf。
这里先设计一个TableBlock类:
@ interface BlockTable: NSObject typedef无效(^ AddCellBlock) (); @ property(原子、复制)AddCellBlock addCell; @end
先这么简单,一个BlockTable只有一个块属性,然后输出一段释放的日志。
@ interface BlockTable: NSObject typedef无效(^ AddCellBlock) (); @ property(原子、复制)AddCellBlock addCell; @end
接着,随意找一个地方写写代码:来了新的一个BlockTable,并打印一下信息:
@ implementation BlockTable - (void) setAddCell: addCell (AddCellBlock) { }
同时去掉置为零的代码:再运行看看:
@ implementation BlockTable - (void) setAddCell: addCell (AddCellBlock) { __weak AddCellBlock addCellWeak=addCell; _addCell=addCellWeak; }
我们先给这个块定义一个弱引用,然后再赋值给_addCell,运行看看:
@ interface BlockTable: NSObject typedef无效(^ AddCellBlock) (); @ property(原子、复制)AddCellBlock addCell; - (void) reloadData; @end @ implementation BlockTable - (void) setAddCell: addCell (AddCellBlock) { __weak AddCellBlock addCellWeak=addCell; _addCell=addCellWeak; } - (void) reloadData { 如果(self.addCell) { self.addCell ();self.addCell();//没事来两次,模拟表多次循环清加细胞 } } - (void) dealloc { NSLog (@“relase一张表”); } @end