NSNotificationCenter:观察者列表? [英] NSNotificationCenter : list of observers?

查看:91
本文介绍了NSNotificationCenter:观察者列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取给定通知名称的观察者列表(对象和选择器)? (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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆