gpt4 book ai didi

ios - NSNotification 中心如何管理它观察对象?

转载 作者:行者123 更新时间:2023-11-28 14:28:55 25 4
gpt4 key购买 nike

在老苹果 guid 中说:“例如,当您向通知中心注册一个对象时,通知中心会存储对该对象的弱引用,并在发布适当的通知时向其发送消息。当对象被释放时,您需要在通知中注销它中心以防止通知中心向不再存在的对象发送任何进一步的消息”在 swift 4 中我们有两种观察方法:1)NotificationCenter.default.addObserver(对象,选择器:2) NotificationCenter.default.addObserver(forName: ... (with block))在第二种情况下,通知中心捕获 block 并保留其中的所有内容,因此在其中使用 self 时要小心

我试图找到关于在 1 种情况下观察者对象如何由通知中心管理的信息,如果你不注销时间?

最佳答案

根据 addObserver(_:selector:name:object:) 的文档:

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 dealloc method. Otherwise, you should call removeObserver(_:name:object:) before observer or any object passed to this method is deallocated.

并且由于调用了观察者的dealloc,所以这里没有内存泄漏。

关于ios - NSNotification 中心如何管理它观察对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51381136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com