Reputation: 7100
I clear out the table view delegate and data source methods directly in dealloc as below:
- (void)dealloc
{
self.tableView.delegate = nil;
self.tableView.dataSource = nil;
}
But looking at some online examples of dealloc, I see that everybody is checking whether the view is loaded before clearing out the delegate and data source like below:
- (void)dealloc
{
if ([self isViewLoaded])
{
self.tableView.delegate = nil;
self.tableView.dataSource = nil;
}
}
Curious to know is it just to check if the memory is allocated to the view, if yes then clear else not. Or is there any specific reason for adding a check here?
Upvotes: 0
Views: 530
Reputation: 6432
What @Wain mentions is right. However, as per the iOS Memory Management Guidelines you should
NEVER use self
to refer to an ivar inside init
or dealloc
precisely for situations like the one he described.
The correct way to do it would be:
- (void)dealloc
{
_tableView.delegate = nil;
_tableView.dataSource = nil;
}
Hope this helps!
Upvotes: 0
Reputation: 119031
If your controller is a table view controller then calling self.tableView
when the view isn't loaded will cause it to load. If you're about to get deallocated then there is no point going to the effort of loading the view. So checking isViewLoaded
is a cheap way of preventing that from happening.
Upvotes: 2