是否可以通过NotificationCenter
列出对象正在观察的所有通知?
答案 0 :(得分:0)
我认为这可能是一个" hacky"方式:使用Swizzling观察NSNotificationCenter.addObserver
方法。
@implementation NSNotificationCenter (Swizzling)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(addObserver:selector:name:object:);
SEL swizzledSelector = @selector(swizzling_addObserver:selector:name:object:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (void)swizzling_addObserver:(id)observer selector:(SEL)aSelector name:(NSNotificationName)aName object:(id)anObject {
//implement code here to store all notifications.
//then call original method (read in the link below to understand why we call **swizzling_addObserver** but not **addObserver**
[self swizzling_addObserver:observer selector:aSelector name:aName object:anObject];
}
要了解有关调配的更多信息,请阅读以下文章: http://nshipster.com/method-swizzling/
答案 1 :(得分:0)
您可以通过解析[[NSNotificationCenter defaultCenter] debugDescription]
并搜索对象地址来实现: