简体   繁体   English

为什么建议将_weak引用与委托一起使用

[英]why it is recommended to use the _weak reference with delegates

Hi every one whether some one will elaborate me that why it is recommended to use the _weak reference for delegates and not the strong reference? 嗨,每个人是否有人会详细说明为什么建议使用_weak参考代表而不是强引用? though we can also use the strong reference for delegates. 虽然我们也可以使用强大的代表参考。 Some one will please tell me with the better and descriptive example with simple way that in which situations should we use the strong reference and in which situations should we use the _weak reference for the delegates. 有些人会用简单的方式告诉我更好和描述性的例子 ,在哪种情况下我们应该使用强引用,在哪种情况下我们应该为代表使用_weak引用。

I went through one of the related question on stack overflow 我经历了一个关于堆栈溢出的相关问题

Is it ever Ok to have a 'strong' reference for a delegate? 为代表提供“强大”的参考资料是否真的好?

but it did not clear my concept properly. 但它没有正确理解我的观念。

Any help will be highly appreciated !! 任何帮助将受到高度赞赏!

Thanks. 谢谢。

Using __strong on delegate is very easy to create retain cycle: 在委托上使用__strong很容易创建保留周期:

say A has a strong reference to B, and some object set A as a delegate of B, if the delegate is strongly referenced, then, a retain cycle is formed. 说A有一个强引用B,有些对象将A设置为B的委托,如果强烈引用委托,则形成一个保留周期。

Yes, It's east to creat a retain cycle while using __strong on delegate. 是的,在委托上使用__strong时创建保留周期是东方的。

Addtionally, in ARC, we using weak on delegate,while the object using the delegate is released,the delegate will be nil automatically. 另外,在ARC中,我们使用弱委托,而使用委托的对象被释放,委托将自动为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM