我正在编写一个小测试程序,我可以查看在iOS设备上发生各种操作时发布的所有通知。不幸的是,当我甚至尝试将观察者添加到达尔文通知中心时,我遇到了一个问题,我得到了一个EXC_BAD_ACCESS错误。相关代码如下:
void callback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
NSNotification* n = [NSNotification notificationWithName:name object:object userInfo:userInfo];
objc_msgSend(gSelf, sel_getUid(@"note:"), n);
return;
}
- (void)viewDidLoad {
[super viewDidLoad];
gSelf = self;
notifications = [[NSMutableArray alloc] initWithCapacity:10];
self.title = @"Notification Log";
CFNotificationCenterAddObserver(
CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
callback, // callback
NULL, // name
NULL, // object
CFNotificationSuspensionBehaviorHold
);
}
-(void)note:(NSNotification *)notification{
@synchronized(self){
[notifications addObject:notification];
[self.tableView reloadData];
}
}
答案 0 :(得分:4)
来自CFNotificationCenter.h :(查找CFNotificationCenterGetDarwinNotifyCenter
的定义。)或the CFNotificationCenter reference
CFNotificationCenterAddObserver():'name'参数可能不是NULL(对于此中心)。