需要知道 NSNotification 对象中的所有监听器

发布于 2024-11-17 13:14:56 字数 71 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈场末日恋爱 2024-11-24 13:14:56

没有;你为什么需要知道?

通知的总体目标是创建一个观察者与通知者的松散耦合系统。 “了解一切”的问题没有意义,因为完全有可能存在来自您根本没有编写的代码的通知的观察者(假设是系统通知)。

当然,如果您创建的通知以您的应用程序唯一命名,那么您还应该知道所有可能的观察者。

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.

哥,最终变帅啦 2024-11-24 13:14:56

如果您想要做的只是调试哪些侦听器正在侦听哪些通知,您可以非常轻松地记录此信息:

NSLog(@"%@", [NSNotificationCenter defaultCenter].debugDescription);

或者

po [NSNotificationCenter defaultCenter]

在调试器中打印出类似以下内容:

<NSNotificationCenter:0x6080000c0070>
Name, Object, Observer, Options
NSWindowWillEnterFullScreenNotification, 0x7fff882e5958, 0x604000100cf0, 1400
NSAntialiasThresholdChangedNotification, 0x7fff882e5958, 0x100e00000, 1400
NSAntialiasThresholdChangedNotification, 0x7fff882e5958, 0x6040001e0300, 1400
NSAntialiasThresholdChangedNotification, 0x7fff882e5958, 0x100f1aa10, 1400
kCFLocaleCurrentLocaleDidChangeNotification, 0x7fff882e5958, 0x7fff882e5a38, 1001
...

If all you want to do is debug which listeners are listening for which notifications, you can log this information pretty easily:

NSLog(@"%@", [NSNotificationCenter defaultCenter].debugDescription);

or

po [NSNotificationCenter defaultCenter]

will print out something like this in the debugger:

<NSNotificationCenter:0x6080000c0070>
Name, Object, Observer, Options
NSWindowWillEnterFullScreenNotification, 0x7fff882e5958, 0x604000100cf0, 1400
NSAntialiasThresholdChangedNotification, 0x7fff882e5958, 0x100e00000, 1400
NSAntialiasThresholdChangedNotification, 0x7fff882e5958, 0x6040001e0300, 1400
NSAntialiasThresholdChangedNotification, 0x7fff882e5958, 0x100f1aa10, 1400
kCFLocaleCurrentLocaleDidChangeNotification, 0x7fff882e5958, 0x7fff882e5a38, 1001
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文