删除观察员是强制性的(必要的)吗?

Is removing observer obligatory (necessary)?

有人说我在整个应用程序生命周期中都需要它,我应该删除它吗? 我认为 GC 会在应用关闭后将其删除,对吗? 如果是,那我什么时候删除它?在 deinit?

如果您要为 iOS 8 及更早版本提供支持。您必须移除 deallocviewWillDisappear 内的观察者。可以找到更详细的答案 here

如果您从 iOS 9 开始提供支持,则不再需要手动删除观察者。来自苹果文档:

In OS X 10.11 and iOS 9.0 NSNotificationCenter and NSDistributedNotificationCenter will no longer send notifications to registered observers that may be deallocated. 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.

可以找到更详细的解释here

注意: 但是在使用上面链接的文档中提到的 block-based 通知时要小心。