Why delegate is never retained?
Why delegate is never retained?
Here is an explanation for why delegates are never retained in Objective-C:
In Objective-C, the delegate pattern is a common design pattern used to implement one-to-one communication between objects. A delegate is an object that acts on behalf of another object, handling tasks and responding to messages on its behalf.
When an object sets another object as its delegate, it typically uses a weak reference to the delegate. This means that the delegate is not retained or owned by the object that sets it. The reason for this is to avoid a strong reference cycle, which can lead to memory leaks.
If an object retained its delegate, and the delegate also had a strong reference back to the object, it would create a strong reference cycle. This means that both objects would hold strong references to each other, preventing them from being deallocated even when no other part of the application is using them.
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.