You need to remove yourself as delegate if your lifespan is shorter than the object you are delegate for. In almost all cases your lifespan is equal to or longer than the object you are delegate for. That said, it's a good habit to get into. Consider the case where you are the delegate for a UITableView. In -init
, perhaps you call:
self.myTableView.delegate = self;
Then it would potentially be wise in -dealloc
to say
_myTableView.delegate = nil;
[_myTableView release];
_myTableView = nil;
The reason to do this is that myTableView may be retained by other objects, so may not deallocate when you release it. If it makes a delegate call after you are gone, your application will crash. So clearing the delegate pointer is a good idea here.
Similarly for NSNotificationCenter, you should remove yourself in -dealloc
thus:
[[NSNotificationCenter defaultCenter] removeObserver:self];
This removes you from all observations. You should do this in -dealloc if your class ever registers for any notifications. If you don't do this, and a notification you were observing comes in after you're gone, the app will crash.
This is not necessary for NSTimers because NSTimers retain their target (you).