How to retrieve all NSNotificationCenter observers?

我是研究僧i 提交于 2019-11-30 18:28:57

Finally, Apple added a way to print all notification center observers:

po [NSNotificationCenter defaultCenter]

It prints a comma separated list with Name, Object, Observer, and Options:

<NSNotificationCenter:0x7f997b307500>
Name, Object, Observer, Options
WebPreferencesRemovedNotification, 0x11165b680, 0x116c87ff8, 1400
UIApplicationWillEnterForegroundNotification, 0x11165b680, 0x7f997a838000, 1400
...
Tomasz Zabłocki

If you don't want to subclass NSNotificationCenter you can rename original addObserver:selector:name:object method and create your own with such name and add observers in there to some array then call original renamed method.

Take a look at following methods: class_addMethod, class_replaceMethod, class_getMethodImplementation.

Also look at this SO question: Method Swizzling

I am not sure why you want observers but you might find this class useful, which removes observers automatically for you which I think might be what you want. SFObservers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!