如何使用CFNotificationCenterGetDarwinNotifyCenter()发送userInfo dict

gra*_*aci 3 core-foundation ios cfnotification

我需要发送一个对象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);
Run Code Online (Sandbox Code Playgroud)

然后我发现在观察者回调中,userInfo dict为NULL.这里有什么我想念的吗?是否有其他方式可以发送userInfo对象?我正在查看http://nshipster.com/inter-process-communication/和分布式通知部分,但每当我使用时:

CFNotificationCenterRef distributedCenter =
CFNotificationCenterGetDistributedCenter();
Run Code Online (Sandbox Code Playgroud)

然后Invalid Use of Function每当我尝试它时都会出现错误错误.

Kur*_*vis 12

如果您阅读了相关文档CFNotificationCenterGetDarwinNotifyCenter,您会发现以下警告:

Darwin通知中心忽略了几个功能参数.为确保将来的兼容性,您应该为所有被忽略的参数传递NULL或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.
Run Code Online (Sandbox Code Playgroud)

为什么?深入挖掘一下.该文档还提到这种类型的通知中心是基于声明的Core OS通知机制构建的notify.h.如果你仔细观察,你会发现只有一种方法可以通过调用发布通知,notify_post()这只需要一个字符串参数.无法向通知添加其他数据.这是一个非常古老而基本的IPC机制,它对CoreFoundation数据结构一无所知.

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