PDA

View Full Version : [NSDictionary description] equivalent for CFDictionary




djRA
Nov 30, 2009, 11:38 PM
Is there an equivalent API for CFDictionary to display its contents the way [NSDictionary description] does?

I was able to dump the CFDictionary using XML by:
CFDataRef tempData = CFPropertyListCreateXMLData(NULL, dict);
CFStringRef tempStrRef = CFStringCreateFromExternalRepresentation(NULL, tempData, kCFStringEncodingASCII);

And tempStrRef gets a plist format of the dictionary but [NSDictionary description] has a different output.

Is there a way to get the same behavior as [description] without having to cast CFDictionary to NSDictionary? I can't use Objective-C code in the program.



kainjow
Dec 1, 2009, 12:11 AM
Try CFCopyDescription()

I can't use Objective-C code in the program.

Why not? Just curious :)

djRA
Dec 1, 2009, 12:34 AM
I'm sorry I forgot to mention but I've already tried CFCopyDescription. It has a different output format even less readable than CFStringCreateFromExternalRepresentation due to the extra hash information and the output has no indention unlike CFStringCreateFromExternalRepresentation and [description].

We can't use Objective-C because we're compiling this code as C (*.c). Why as C, you might ask. We're trying to keep it as simple and sort of low-level-ish.

Is it possible to invoke [NSDictionary/NSObject description] in C syntax? Like objc_msgsend() or something like that? :D

kainjow
Dec 1, 2009, 08:42 AM
You could create a separate .h/.m file with a function that calls [dict description] and return it as a CFStringRef.

Why do you need this in the first place? It shouldn't really be used anywhere else but for debugging purposes, according to the docs since the format could change.

lee1210
Dec 1, 2009, 10:03 AM
It sounds to me like you should just write a C function that iterates through the key/value pairs in a CFDictionary and builds a CFString with the exact format you want. Then you don't have to rely on all of this silliness.

-Lee

Catfish_Man
Dec 1, 2009, 12:32 PM
We can't use Objective-C because we're compiling this code as C (*.c). Why as C, you might ask. We're trying to keep it as simple and sort of low-level-ish.


Complicating things to keep them simple seems a bit nonsensical. Just rename to .m and use -description.


Is it possible to invoke [NSDictionary/NSObject description] in C syntax? Like objc_msgsend() or something like that? :D

This is just using ObjC in a more complicated way. You're not fooling the compiler :P

chown33
Dec 1, 2009, 01:08 PM
We can't use Objective-C because we're compiling this code as C (*.c). Why as C, you might ask. We're trying to keep it as simple and sort of low-level-ish.

Is it possible to invoke [NSDictionary/NSObject description] in C syntax? Like objc_msgsend() or something like that? :D

Nothing like deception to really complicate things.

Isolate the function that returns the description. Implement it in .m or .c or whatever. The function declaration stays the same:
extern CFStringRef dictDescription( CFDictionaryRef dict );



Why does the string have to be exactly in the -description format? Is it for debugging? Pfah, do whatever works. Is it for exporting to a particular format? Write your own function, like Lee1210 suggested (because -description's output can change, and has in the past). Is the string for something else? What?

Muncher
Dec 1, 2009, 06:41 PM
Is it possible to invoke [NSDictionary/NSObject description] in C syntax? Like objc_msgsend() or something like that? :D

Using objc_msgsend() doesn't change anything. You can look at some of the optimizations here (http://www.mulle-kybernetik.com/artikel/Optimization/opti-3.html) though. Why do you need the extra speed? Running something heavy in real-time?