I am trying to display the value stored a particular value of enum using NSLog. In the following example, I am trying to get the output as: 5 represents month of May
.
我试图使用NSLog显示存储特定枚举值的值。在下面的示例中,我尝试将输出设置为:5表示五月份。
Any idea what is the right token to use for enum
with NSLog? I tried using %i and %@, but both do not work.
知道什么是使用NSLog枚举的正确令牌?我尝试使用%i和%@,但两者都不起作用。
Thanks!
谢谢!
enum month {jan = 1, feb, march, apr, may, jun, jul, aug, sep, oct, nov, dec};
enum month amonth;
int x = 5;
amonth = x;
NSLog(@"%i represents month of %@", x,amonth);
1 个解决方案
#1
9
Unfortunately, what you're asking for is not possible. Enum names are not preserved past compilation (except as debugging information available to the compiler). So unless you want to a) ship debugging information in your app, and b) effectively write a debugger inside your app that uses the embedded debugging information, it's just not going to work.
不幸的是,你所要求的是不可能的。枚举名称在编译之后不会保留(编译器可用的调试信息除外)。因此,除非您想要a)在您的应用程序中发布调试信息,并且b)在您的应用程序中有效地编写使用嵌入式调试信息的调试器,它就不会起作用。
The typical solution to this problem is to provide a function that returns the appropriate name, using a switch
statement.
此问题的典型解决方案是使用switch语句提供返回相应名称的函数。
NSString *monthName(enum month m) {
switch (m) {
case jan:
return @"jan";
case feb:
return @"feb";
...
}
return @"unknown";
}
One benefit of this approach is you can localize the names.
这种方法的一个好处是您可以本地化名称。
#1
9
Unfortunately, what you're asking for is not possible. Enum names are not preserved past compilation (except as debugging information available to the compiler). So unless you want to a) ship debugging information in your app, and b) effectively write a debugger inside your app that uses the embedded debugging information, it's just not going to work.
不幸的是,你所要求的是不可能的。枚举名称在编译之后不会保留(编译器可用的调试信息除外)。因此,除非您想要a)在您的应用程序中发布调试信息,并且b)在您的应用程序中有效地编写使用嵌入式调试信息的调试器,它就不会起作用。
The typical solution to this problem is to provide a function that returns the appropriate name, using a switch
statement.
此问题的典型解决方案是使用switch语句提供返回相应名称的函数。
NSString *monthName(enum month m) {
switch (m) {
case jan:
return @"jan";
case feb:
return @"feb";
...
}
return @"unknown";
}
One benefit of this approach is you can localize the names.
这种方法的一个好处是您可以本地化名称。