Alright, I wrote up a little example of CFNotificationCenter. Generally, nobody uses CoreFoundation for large projects, and instead use Foundation. If you are really writing this project in Objective-C (as I assume from your tags), I would suggest using NSNotificationCenter. Without further adue, here is the example:
#include <CoreFoundation/CoreFoundation.h>
void notificationCallback (CFNotificationCenterRef center,
void * observer,
CFStringRef name,
const void * object,
CFDictionaryRef userInfo) {
CFShow(CFSTR("Received notification (dictionary):"));
// print out user info
const void * keys;
const void * values;
CFDictionaryGetKeysAndValues(userInfo, &keys, &values);
for (int i = 0; i < CFDictionaryGetCount(userInfo); i++) {
const char * keyStr = CFStringGetCStringPtr((CFStringRef)&keys[i], CFStringGetSystemEncoding());
const char * valStr = CFStringGetCStringPtr((CFStringRef)&values[i], CFStringGetSystemEncoding());
printf(" "%s" = "%s"
", keyStr, valStr);
}
}
int main (int argc, const char * argv[]) {
CFNotificationCenterRef center = CFNotificationCenterGetLocalCenter();
// add an observer
CFNotificationCenterAddObserver(center, NULL, notificationCallback,
CFSTR("MyNotification"), NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
// post a notification
CFDictionaryKeyCallBacks keyCallbacks = {0, NULL, NULL, CFCopyDescription, CFEqual, NULL};
CFDictionaryValueCallBacks valueCallbacks = {0, NULL, NULL, CFCopyDescription, CFEqual};
CFMutableDictionaryRef dictionary = CFDictionaryCreateMutable(kCFAllocatorDefault, 1,
&keyCallbacks, &valueCallbacks);
CFDictionaryAddValue(dictionary, CFSTR("TestKey"), CFSTR("TestValue"));
CFNotificationCenterPostNotification(center, CFSTR("MyNotification"), NULL, dictionary, TRUE);
CFRelease(dictionary);
// remove oberver
CFNotificationCenterRemoveObserver(center, NULL, CFSTR("TestValue"), NULL);
return 0;
}
This example creates an observer, posts a simple dictionary to it, and removes the observer. More information on CFNotificationCenter can be found on Apple's CFNotificationCenter Reference.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…