如何使用CFNotificationCenterGetDarwinNotifyCenter()发送一个userInfo字典

我需要使用CFNotificationCenterGetDarwinNotifyCenter()发送一个对象,但是我注意到,每当我发送一个通知使用

 const void *tkeys[1] = { @"testKey" }; const void *tvalues[1] = { @"testValue" }; CFDictionaryRef userInfo = CFDictionaryCreate(NULL, tkeys, tvalues, 1, NULL, NULL); CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("foo"), object, userInfo, true); 

那么我发现在观察者callback中,userInfo字典是NULL。 有什么我在这里失踪? 有没有其他的方式,我可以发送userInfo对象? 我在看http://nshipster.com/inter-process-communication/和分布式通知部分,但是每当我使用:

 CFNotificationCenterRef distributedCenter = CFNotificationCenterGetDistributedCenter(); 

那么每当我尝试它时,我都会收到错误“ Invalid Use of Function错误”。

如果你阅读CFNotificationCenterGetDarwinNotifyCenter的文档 ,你会发现这样的警告:

达尔文通知中心忽略了几个函数参数。 为确保将来的兼容性,您应该为所有被忽略的parameter passingNULL或0。

userInfo是其中的一个参数。 CFNotificationCenter.h更详细地介绍:

 // For this center, there are limitations in the API. There are no notification "objects", // "userInfo" cannot be passed in the notification, ... // - CFNotificationCenterPostNotification(): the 'object', 'userInfo', and 'deliverImmediately' arguments are ignored. 

为什么? 深入一点。 文档还提到,这种types的通知中心是在notify.h声明的Core OS通知机制上notify.h 。 如果你仔细看,你会发现只有一种方法来发布通知,通过调用notify_post() ,只接受一个string参数。 没有办法将其他数据添加到通知。 这是一个非常古老和基本的IPC机制,不知道任何有关CoreFoundation数据结构。

至于CFNotificationCenterGetDistributedCenter ,这在iOS上不可用。