Previously, this was impossible (you have to write it all out by hand / create a static array / put all the values into a dictionary and read them back ... etc)
But I've noticed that the latest Xcode's lldb (4.6, maybe earlier versions too) is automatically converting enum-constants to strings.
My problem is that we use a lot of libraries - including Apple's own! - which use annoying public enums with no "value-to-string" method offered. So I end up having to (many, many times over) do the "well, since Mr. Library Author didn't do this, now I have to make the static array for them...".
I kept hoping Apple would provide a way out of this - is it finally here? Or is this some trick that only the debugger can do - mere runtime code has no access to it?
lldb doesn't have any special capabilities regarding printing enum names. I think what you're seeing is the result of the enum values being recorded in the debug info (or not). For instance,
If you look at the debug info for this binary (
dwarfdump a.out.dSYM
) you'll see that the variablevar
's type ismyenums
and the debug information includes the values of those enumerated types:If I add another enum to my sample file which isn't used anywhere,
re-compile and look at the DWARF again via
dwarfdump
, I won't find any debug info describingotherenums
- it is unused (in this compilation unit) and so it is elided.As has been noted, the constant names are not accessible after compilation except in the debugger. H2CO3's suggestion of the
TO_STR()
macro should get you through most uses, I imagine.I was looking into libclang recently and decided to exercise it by addressing your complaint about the drudgery of translating from enum value to string "(you have to write it all out by hand / create a static array / put all the values into a dictionary and read them back ... etc)".
Here's a Python script that will parse a Cocoa file, find the enums (including those defined with
NS_OPTIONS
andNS_ENUM
), and spit out either an array or a function (using aswitch
) to do the mapping. The array option takes advantage of a C feature called "designated (array) initializers", whereby array members can be explicitly paired with a particular index:Note that this is not a sparse array -- any non-specified indexes before the last are still created, and initialized with 0. The implication is that enums intended to be used as bitmasks, whose values are
1 << 2
,1 << 3
,1 << 4
, and so on, will create fairly large arrays for relatively few used values. A function is probably the better choice in those cases.Anonymous
enum
s (which I think are all single-member, in Foundation at least) are converted directly to a singleNSString
using the name of the constant. I've punted on the automatic handling of constants that are negative-valued, likeNSOrderedAscending
-- a negative index in the array initializer won't compile, but the function/switch
alternative will work fine; you just have to choose that manually for those few cases.The script is up on GitHub, and here it is in its entirety. MIT license, so do with it what you like. I'll be happy to hear about any modifications.