[NSDictionary description] equivalent for CFDictionary

Discussion in 'Mac Programming' started by djRA, Nov 30, 2009.

  1. djRA macrumors newbie

    Nov 30, 2009
    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.
  2. kainjow Moderator emeritus


    Jun 15, 2000
    Try CFCopyDescription()

    Why not? Just curious :)
  3. djRA thread starter macrumors newbie

    Nov 30, 2009
    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
  4. kainjow Moderator emeritus


    Jun 15, 2000
    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.
  5. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    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.

  6. Catfish_Man macrumors 68030


    Sep 13, 2001
    Portland, OR
    Complicating things to keep them simple seems a bit nonsensical. Just rename to .m and use -description.

    This is just using ObjC in a more complicated way. You're not fooling the compiler :p
  7. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    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?
  8. Muncher macrumors 65816


    Apr 19, 2007
    Using objc_msgsend() doesn't change anything. You can look at some of the optimizations here though. Why do you need the extra speed? Running something heavy in real-time?

Share This Page