I know , the delegate is never retained! Ever!
But can anyone explain me why delegate is never retained ?...
Thanx in advance
The rule is to not retain it because it's already retained elsewhere and more important you'll avoid retain cycles.
When you define a delegate object as property, it's used a weak reference in the object it is defined in(lets say A, i.e. the delegate object is a property of A). The delegate is assigned when you init A(let's say in B), then most likely you would assign A. delegate to self, which is acturally B.
It's a memory management thing.
Objective-C works with reference counts to keep the memory clean. This does mean that it can't detect cyclic relationships.
Example:
It's up to you. If you declare it to be retained (strong
in ARC) it'll be retained.
The rule is to not retain it because it's already retained elsewhere and more important you'll avoid retain cycles.
To expand on djromero's answer:
If you have a UIViewController
which contains a UITableView
, the controller will be most likely retaining the table and it will be it's delegate / dataSource. If the table retains the delegate / dataSource, then they will be retaining each other and thus never getting released.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With