枚举值NSString(iOS) [英] enum Values to NSString (iOS)
问题描述
枚举{value1,value2,value3} myValue;
在我应用程序的某一点上,我希望检查该枚举的哪个值现在是活动的。我正在使用NSLog,但我不清楚如何显示枚举的当前值(value1 / valu2 / valu3 / etc ...)作为NSLog的NSString。
任何人?
这里回答:关于实现的几个建议
底线是 Objective-C
正在使用常规旧的 C
枚举
给定一个枚举
像这样:
typedef枚举{a,b,c} FirstThreeAlpha;
您的方法如下所示:
- (NSString *)convertToString:(FirstThreeAlpha)whichAlpha {
NSString * result = nil;
switch(whichAlpha){
case a:
result = @a;
break;
案例b:
result = @b;
break;
case c:
result = @c;
break;
默认值:
result = @unknown;
}
返回结果;
}
I have an enum holding several values:
enum {value1, value2, value3} myValue;
In a certain point in my app, I wish to check which value of the enum is now active. I'm using NSLog but I'm not clear on how to display the current value of the enum (value1/valu2/valu3/etc...) as a NSString for the NSLog.
Anyone?
This is answered here: a few suggestions on implementation
The bottom line is Objective-C
is using a regular, old C
enum
, which is just a glorified set of integers.
Given an enum
like this:
typedef enum { a, b, c } FirstThreeAlpha;
Your method would look like this:
- (NSString*) convertToString:(FirstThreeAlpha) whichAlpha {
NSString *result = nil;
switch(whichAlpha) {
case a:
result = @"a";
break;
case b:
result = @"b";
break;
case c:
result = @"c";
break;
default:
result = @"unknown";
}
return result;
}
这篇关于枚举值NSString(iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!