需要知道 NSNotification 对象中的所有监听器
我正在使用 NSNotificationCenter 对象。我需要了解所有听众以及他们收听的事件。我可以做些什么来获得此列表吗?
I am using NSNotificationCenter object. I need to know all the listeners and the events they listen to. Is there anything that I can do to get this listing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有;你为什么需要知道?
通知的总体目标是创建一个观察者与通知者的松散耦合系统。 “了解一切”的问题没有意义,因为完全有可能存在来自您根本没有编写的代码的通知的观察者(假设是系统通知)。
当然,如果您创建的通知以您的应用程序唯一命名,那么您还应该知道所有可能的观察者。
Nope; why do you need to know?
The whole goal of notifications is to create a loosely couple system of observers vs. notifiers. The very question of "knowing all" doesn't make sense as it is perfectly possible that there will be observers of the notification that come from code you didn't author at all (assuming a system notification).
And, of course, if it is a notification that you create that is named uniquely to your app, then you should also know all possible observers.
如果您想要做的只是调试哪些侦听器正在侦听哪些通知,您可以非常轻松地记录此信息:
或者
在调试器中打印出类似以下内容:
If all you want to do is debug which listeners are listening for which notifications, you can log this information pretty easily:
or
will print out something like this in the debugger: