Pass NSObject in CFNotification

How can I pass a NSObject to another class using CFNotifications? I need to use CFNotifications for what I'm trying to do, so NSNotifications will not work.

I thought about converting a NSObject to a kind of CFString id of that object, and then recreating the NSObject on the other process (that receives the CFNotification) with that CFString, but I have no idea on how to do that.

Please help,

Pedro Franceschi.

Upvotes: 0

Views: 596

Answers (1)

Alexsander Akers
Alexsander Akers

Reputation: 16024

With CFNotificationCenterPostNotification you can do this:

// ~~~~~~~~~~~~~~~~~~~
// SOMEWHERE IN A FUNCTION OR METHOD:
// ~~~~~~~~~~~~~~~~~~~

id object = /* Assume this exists */;
CFNotificationCenterPostNotification(aCenter, CFSTR("NotificationName"), (__bridge void *) object, NULL, true);

// ~~~~~~~~~~~~~~~~~~~
// LATER IN YOUR CODE:
// ~~~~~~~~~~~~~~~~~~~

void receivedNotification(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
    id object = (__bridge id) object;
    /* Do stuff with `object`. */
}

Leave out the __bridges if you're not using ARC.

Upvotes: 1

Related Questions