Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html" rel="noreferrer">NSNotificationCenter</a>. Without further adue, here is the example:</p> <pre><code>#include &lt;CoreFoundation/CoreFoundation.h&gt; 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, &amp;keys, &amp;values); for (int i = 0; i &lt; CFDictionaryGetCount(userInfo); i++) { const char * keyStr = CFStringGetCStringPtr((CFStringRef)&amp;keys[i], CFStringGetSystemEncoding()); const char * valStr = CFStringGetCStringPtr((CFStringRef)&amp;values[i], CFStringGetSystemEncoding()); printf("\t\t \"%s\" = \"%s\"\n", 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, &amp;keyCallbacks, &amp;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; } </code></pre> <p>This example creates an observer, posts a simple dictionary to it, and removes the observer. More information on CFNotificationCenter can be found on <a href="http://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFNotificationCenterRef/Reference/reference.html" rel="noreferrer">Apple's CFNotificationCenter Reference</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload