tags:

views:

169

answers:

2

i have a .m class written in objective c that needs to send information to a .mm class written in c++

i tried to use CFNotification and as far as i can tell the notifcation sent by the .m class can be picked up by the .mm class

the problem now is trying to get information across in that notification. The documentation says i need to pass the info as a CFDictionary, i'm having a lot of trouble trying to create the dictionary...

the problem is due because of the second and third parameter. They require a const void** type... which is a c++ array (right?) and i can't make a c++ array that contains some objective c objects

i tried making a c++ class in another .mm file but i can't include that in the .m file because i get compile errors saying there is stuff wrong with the syntax. if i change the .m file to a .mm file i get rid of those errors but i get a whole lot of other errors from the openGL and a few other conversion compile errors

... i tried making a .m wrapper for the .mm file so i can send and receive NSNotifications but i get a stupid error saying i've tried to redeclare the type of the wrapper but only when i tried using it...

A: 

Okay, you're making this way harder than it needs to be.

So you want to send a notification with Objective-C? Use NSNotificationCenter. That's it, you're done. Don't use CFNotificationCenter. Actually, they're the same thing, but you shoud use the NSNotificationCenter interface from Objective C and only use the CFNotificationCenter interface when you need to write something in plain old C -- approximately never, if your app already has Objective C in it.

The void const** isn't necessarily a C++ array, either. And yes, you CAN put Objective-C objects in normal C arrays. For example:

NSString *const MESSAGES[2] = { @"Hello", @"Goodbye" };
Dietrich Epp
+1  A: 

The easiest solution to your problem is to forget about Objective C files and C++ files and switch to using Objective C++ files (extension .mm) for everything. You can alternatively tell Xcode to compile everything as Objective C++ irrespective of file extension.

Then use the fact that NSNotificationCenter and CFNotificationCenter interoperate, so you can use whichever you prefer at the time, or since your code is now all Objective C++, you can use either one exclusively from everywhere.

Follow that up with the fact that NSDictionary* and CFDictionaryRef are toll free bridged, as are NSString* and CFStringRef. That is to say, anything that accepts a CFDictionaryRef as a parameter can be passed an NSDictionary* cast to a CFDictionaryRef and vice versa.

So if you have an NSDictionary with NSString keys and values you can equally treat that pointer as a CFDictionaryRef with CFStringRef keys and values.

Peter N Lewis