将Objective-C枚举常量转换为字符串名称 [英] Convert Objective-C enum constants to string names

查看:248
本文介绍了将Objective-C枚举常量转换为字符串名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,这是不可能的(您必须手动将其全部写出/创建一个静态数组/将所有值放入字典中并读回它们……等等)

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)

但是我注意到最新的Xcode的lldb(4.6,也许还有更早的版本)正在自动将枚举常量转换为字符串.

But I've noticed that the latest Xcode's lldb (4.6, maybe earlier versions too) is automatically converting enum-constants to strings.

我的问题是我们使用了很多库-包括Apple自己的库! -使用烦人的公共枚举,但没有提供按字符串数值转换"方法.因此,我最终不得不(很多次)做了好吧,因为图书馆作者先生没有这样做,现在我必须为他们制作静态数组...".

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在打印枚举名称方面没有任何特殊功能.我认为您所看到的是枚举值记录在调试信息中的结果(或没有).例如,

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,

enum myenums {a = 0, b, c};
int main ()
{
 enum myenums var = b;
 return (int) var;  // break here
}

% xcrun clang -g a.c
% xcrun lldb a.out
(lldb) br s -p break
Breakpoint 1: where = a.out`main + 18 at a.c:5, address = 0x0000000100000f92
(lldb) r
[...]
-> 5     return (int) var;  // break here
   6    }
(lldb) p var
(myenums) $0 = b
(lldb) p (myenums) 0
(myenums) $1 = a
(lldb) 

如果查看此二进制文件(dwarfdump a.out.dSYM)的调试信息,您会看到变量var的类型为myenums,并且调试信息包括以下枚举类型的值:

If you look at the debug info for this binary (dwarfdump a.out.dSYM) you'll see that the variable var's type is myenums and the debug information includes the values of those enumerated types:

0x0000005a:     TAG_enumeration_type [5] *
                 AT_name( "myenums" )
                 AT_byte_size( 0x04 )
                 AT_decl_file( "/private/tmp/a.c" )
                 AT_decl_line( 1 )

0x00000062:         TAG_enumerator [6]  
                     AT_name( "a" )
                     AT_const_value( 0x0000000000000000 )

0x00000068:         TAG_enumerator [6]  
                     AT_name( "b" )
                     AT_const_value( 0x0000000000000001 )

0x0000006e:         TAG_enumerator [6]  
                     AT_name( "c" )
                     AT_const_value( 0x0000000000000002 )

如果我在示例文件中添加了另一个未在任何地方使用的枚举,

If I add another enum to my sample file which isn't used anywhere,

enum myenums {a = 0, b, c};
enum otherenums {d = 0, e, f}; // unused in this CU
int main ()
{
 enum myenums var = b;
 return (int) var;  // break here
}

重新编译并通过dwarfdump再次查看DWARF,我找不到任何描述otherenums的调试信息-它未使用(在此编译单元中),因此被删除了.

re-compile and look at the DWARF again via dwarfdump, I won't find any debug info describing otherenums - it is unused (in this compilation unit) and so it is elided.

这篇关于将Objective-C枚举常量转换为字符串名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆