NSNotificationCenter:观察者列表? [英] NSNotificationCenter : list of observers?
问题描述
是否可以获取给定通知名称的观察者列表(对象和选择器)? (NSNotificationCenter)
Is it possible to get the list of observers (objects and selectors) for a given notification name? (NSNotificationCenter)
推荐答案
(iOS 9,Swift 3)如果要找出当前在NotificationCenter
中注册了哪些观察者,请中断并打印其调试描述:
(iOS 9, Swift 3) If you want to find out which observers are currently registered in NotificationCenter
, break and print its debug description:
(lldb) e print(NotificationCenter.default.debugDescription)
输出的每一行将包含(通知)名称,对象,观察者,选项.使用某些NSNotification.Name
多次调用NotificationCenter.default.addObserver
将会在此列表中产生多个条目.
Each line of the output will contain (Notification) Name, Object, Observer, Options. Multiple calls to NotificationCenter.default.addObserver
with some NSNotification.Name
will result in multiple entries in this list.
NB.尽管这可能在调试时证明有用的信息,但我不建议在运行时使用此输出来管理观察者.
NB. while this might prove useful information when debugging, I would not advise to manage observers at runtime using this output.
(来源:答案基于 useyourloaf )
这篇关于NSNotificationCenter:观察者列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!