2017-06-26 8 views

答えて

0

「ハッキー」な方法で可能です:スウィズルを使って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/

0

あなたは[[NSNotificationCenter defaultCenter] debugDescription]を解析し、オブジェクトのアドレスを検索することによってそれを行うことができます:

  • 名、オブジェクト、オブザーバー、オプション
  • NSWindowWillEnterFullScreenNotification、0x7fffb685e8e0、 0x6000001003f0,1400
  • _NSWindowDidChangeContentsHostedInLayerSurfaceNotification、0x7fffb685e8e0、0x6180000a4680、1400
  • NSAntialiasThresholdChangedNotification、0x7fffb685e8e0、 0x6080001e0300、1400
  • kCFLocaleCurrentLocaleDidChangeNotification、 0x7fffb685e8e0、0x7fffb685e9c0、1001
  • some_string、0x7fffb685e8e0、 0x600000000210、1400
  • ...
関連する問題