美文网首页
iOS中的循环引用问题

iOS中的循环引用问题

作者: Keep_FighterLS | 来源:发表于2016-03-30 16:35 被阅读1611次

如题,最近检查代码,慢慢的填之前挖过的坑,发现之前写的代码真是差到不忍直视。发现很多循环引用问题。

容易引起循环引用的问题总结

  • 1.delegate的写法,应按照如下写法,切记不可存在strong标示符。
@property (nonatomic, weak, nullable) id <UICollectionViewDelegate> delegate;
@property (nonatomic, weak, nullable) id <UICollectionViewDataSource> dataSource;
  • 2.单例里面的变量。
    错误写法,然后将self传入,导致释放不掉。
@property (nonatomic,strong) UIViewController *mViewController;
  • 3.block里面注意弱引用self即可。
  • 4.传值时注意被赋值的变量。
UIViewController *vc;//默认为strong类型,同样可写为UIViewController __strong *vc
UIViewController __weak *vc;//通过__weak弱引用变量。

5.NSTimer
会对传入的target强引用,具体参看Weak Reference to NSTimer Target To Prevent Retain Cycle
解决办法有两个:
a.GCD完成NSTimer的功能
https://github.com/mindsnacks/MSWeakTimer
b.用一个中间对象处理
YYWeakProxy

相关文章

网友评论

      本文标题:iOS中的循环引用问题

      本文链接:https://www.haomeiwen.com/subject/svfelttx.html