where to remove observer in swift

https://stackoverflow.com/questions/28689989/where-to-remove-observer-for-nsnotification-in-swift/40339926#40339926
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Deinitialization.html

Swift automatically deallocates your instances when they are no longer needed, to free up resources. Deinitializers are called automatically, just before instance deallocation takes place. You are not allowed to call a deinitializer yourself.

deinitializion in class hierarchy

Superclass deinitializers are inherited by their subclasses, and the superclass deinitializer (parent) is called automatically at the end of a subclass deinitializer (child) implementation. Hence, the lowest level (youngest kids) subclass calls their deinit. Then their parents. Then the parent’s parents…and so on.

Also, superclass deinitializers are always called, even if a subclass does not provide its own deinitializer.

As of iOS 9

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.

In detail:
https://useyourloaf.com/blog/unregistering-nsnotificationcenter-observers-in-ios-9/

Using iOS 9 or later the Foundation framework release notes contain some good news:

In OS X 10.11 and iOS 9.0 NSNotificationCenter and NSDistributedNotificationCenter will no longer send notifications to registered observers that may be deallocated.
The notification center now keeps a zeroing reference to the observer:

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.
So the next time the notification center wants to send a notification to the observer it can detect that it no longer exists and remove the observer for us:

This means that observers are not required to un-register in their deallocation method. The next notification that would be routed to that observer will detect the zeroed reference and automatically un-register the observer.

Note that this does not apply if you are using a block based observer:

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.