[英]How can I send a userInfo dict using CFNotificationCenterGetDarwinNotifyCenter()
我需要使用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);
然后我發現在觀察者回調中,userInfo dict為NULL。 這里有什么我想念的嗎? 是否有其他方式可以發送userInfo對象? 我正在查看http://nshipster.com/inter-process-communication/和分布式通知部分,但每當我使用時:
CFNotificationCenterRef distributedCenter =
CFNotificationCenterGetDistributedCenter();
然后Invalid Use of Function
每當我嘗試它時,我都會收到錯誤Invalid Use of Function
錯誤。
如果您閱讀了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.
為什么? 深入挖掘一下。 該文檔還提到這種類型的通知中心是基於notify.h
聲明的Core OS通知機制notify.h
。 如果你仔細觀察,你會發現只有一種方法可以發布通知,方法是調用notify_post()
,它只需要一個字符串參數。 無法向通知添加其他數據。 這是一個非常古老而基本的IPC機制,它對CoreFoundation數據結構一無所知。
至於CFNotificationCenterGetDistributedCenter
,這在iOS上不可用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.