美文网首页
UIView 使用weak、strong的区别

UIView 使用weak、strong的区别

作者: shopping627 | 来源:发表于2016-12-05 17:19 被阅读75次

weak、strong最大区别在于 removeFromSuperview这个方法:

weak在父view中持有的计数器为1,即superview

strong在父view中持有的计数器为2,即superview 和 strong属性

因此,当调用removeFromSuperview时,weak所引用的计数器为0,view会被释放,而strong view则不会。在业务中,如果把view移除了,又要加回来的,应该使用strong view,weak view 的优势在于可以及时释放内存。

相关文章

网友评论

      本文标题:UIView 使用weak、strong的区别

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