Und*_*ion 5 logging objective-c nsnotification nsnotificationcenter ios
我想记录由我的应用程序共享的单个NSNotificationCenter发布的任何NSNotifications.我已经尝试了子类化NSNotificationCenter,意图将日志代码添加到三个post方法,但它返回CFNotification中心的实例而不是我的子类.
当然有一种监控NSNotification发布的方法吗?
编辑/ UPDATE
正如下面的两个答案正确地指出我可以监听所有通知并将它们记录在处理程序中,但处理程序将接收这些通知的顺序远不能保证与它们被分派的顺序相同.如果我可以确定处理程序将始终是第一个被通知的处理程序,这将起作用,但我不能:'观察者接收通知的顺序未定义'来自NSNotification Docs
通过使用- addObserver:selector:name:object:并通过nil对双方name和object,你会收到通知的任何通知.
- (id)init
{
    self = [super init];
    if (self != nil)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(log:) name:nil object:nil];
    }
    return self;
}
- (void)log:(NSNotification *)notification
{
    NSLog(@"%@", notification);
}
编辑:如果要获取正在发送的通知的实际顺序,请尝试子类化NSNotificationCenter并覆盖以下方法:
– postNotification:
– postNotificationName:object:
– postNotificationName:object:userInfo:
如果子类化不适合您,您可以考虑NSNotificationCenter通过调用超级实现来定义覆盖这些方法的类别.(你需要调整方法来在一个类别中调用super).如果您需要帮助,请告诉我.
| 归档时间: | 
 | 
| 查看次数: | 1034 次 | 
| 最近记录: |