deinit {
// Release all resources
// perform the deinitialization
}
A deinitializer is called immediately before a class instance is deallocated. You write deinitializers with the deinit keyword, similar to how intializers are written with the init keyword. Deinitializers are only available on class types.
As of iOS 9 (and OS X 10.11), you don't need to remove observers yourself, if you're not using block based observers though. The system will do it for you, since it uses zeroing-weak references for observers, where it can.
And if you are using block based observers, make sure you capture self weakly using [weak self] in the closure's capture list, and remove observer in deinit method. If you don't use weak reference to self, deinit method (and thus removal of that observer) will never be called since Notification Center will hold a strong reference to it indefinitely.
If the observer is able to be stored as a zeroing-weak reference the underlying storage will store the observer as a zeroing weak reference, alternatively if the object cannot be stored weakly (i.e. it has a custom retain/release mechanism that would prevent the runtime from being able to store the object weakly) it will store the object as a non-weak zeroing reference. This means that observers are not required to un-register in their deallocation method.
Block based observers via the -[NSNotificationCenter addObserverForName: object: queue: usingBlock] method still need to be un-registered when no longer in use since the system still holds a strong reference to these observers.
I have a chat application, so whenever I go from my ChatLogViewController to some other viewController and then comeback, I have 1 extra Observer of my keyboard notification. To remove that I remove all the observers when I change my viewController or disappear from my chatLogViewController.