且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么使用弱指针进行委派?

更新时间:2023-11-13 23:51:40

对象弱地保留其委托的原因是为了避免保留周期.想象以下情况:对象a创建b并保留它,然后将其自身设置为b的委托. a由其所有者释放,保留一个包含ab的保留周期.这实际上是一个非常常见的情况.考虑一个拥有一个视图并充当该视图的委托的视图控制器.在这种情况下,视图不应保留控制器,这是适当的MVC架构的主要内容,并应防止保留周期.

The reason that objects weakly retain their delegates is to avoid retain cycles. Imagine the following scenario: object a creates b and retains it, then sets itself as b's delegate. a is released by its owner, leaving a retain cycle containing a and b. This is actually a very common scenario. Consider a view controller that owns a view and acts as that view's delegate. In this case, the view should not retain the controller—as a mater of proper MVC architecture and to prevent retain cycles.