How to retrieve all NSNotificationCenter observers?

前端 未结 2 1775
栀梦
栀梦 2021-01-04 06:01

I\'d like to retrieve a list of observers (objects and selectors) for a given notification name. I know there\'s no official API for that. I also know I could subclass NSNot

2条回答
  •  再見小時候
    2021-01-04 06:14

    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

提交回复
热议问题