iOS中通知需要移除吗?

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSNotificationName)aName object:(nullable id)anObject;

我们看看Apple对这个方法的解释:

Adds an entry to the receiver’s dispatch table with an observer, a notification selector and optional criteria: notification name and sender.

If your app targets iOS 9.0 and later or macOS 10.11 and later, you don't need to unregister an observer in its deallocation method. If your app targets earlier releases, be sure to invoke removeObserver:name:object: before observer or any object specified in addObserver:selector:name:object: is deallocated.

Apple说明这个方法会把“an observer, a notification selector and optional criteria: notification name and sender.”作为参数,给receiver的调度列表添加一个入口。如果app的目标用户是iOS 9.0 and later or macOS 10.11及之后,我们不需要在dealloc方法中取消注册者观察者,如果你的APP目标用户要稍早一些,要确保观察者在dealloc前调用remove方法。

结论:目前大家最低支持的是iOS8,因此还是需要移除的。等明年就不需要了,因为明年就可以不支持iOS8了,直接支持iOS9了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容