Objective-c: NSString 枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/925991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective-c: NSString to enum
提问by Hectoret
So, I've got this definition:
所以,我有这个定义:
typedef enum {
red = 1,
blue = 2,
white = 3
} car_colors;
Then, I've got a variable of type car_colors: car_colors myCar;
然后,我有一个 car_colors 类型的变量: car_colors myCar;
The question is, I receive the color of the car in a NSString. It must be a NSString, I can't change that. How can I convert from NSString to car_colors type?
问题是,我在 NSString 中收到汽车的颜色。它必须是一个 NSString,我不能改变它。如何从 NSString 转换为 car_colors 类型?
NSString *value = [[NSString alloc] initWithString:@"1"];
myCar = [value intValue]; // <-- doesn't work
any idea? thanks!
任何的想法?谢谢!
采纳答案by Abizern
Rather than use an array, why not use a dictionary; You have the colour NSString as keys, and you return whatever NSNumber you want. Something like; (Long winded for clarity).
与其使用数组,不如使用字典;您将颜色 NSString 作为键,并返回您想要的任何 NSNumber。就像是; (为清晰起见,长篇大论)。
NSDictionary *carColourDictionary = @{@"Red": @1,
@"Blue": @2,
@"White": @3};
// Use the dictionary to get the number
// Assume you have a method that returns the car colour as a string:
// - (NSString *)colourAsString;
int carColour = carColourDictionary[object colourAsString];
回答by
here's an implementation using NSDictionaryand the existing enum
这是使用NSDictionary和现有枚举的实现
in .h file:
在 .h 文件中:
typedef NS_ENUM(NSInteger, City) {
Toronto = 0,
Vancouver = 1
};
@interface NSString (EnumParser)
- (City)cityEnumFromString;
@end
in .m file:
在 .m 文件中:
@implementation NSString (EnumParser)
- (City)cityEnumFromString{
NSDictionary<NSString*,NSNumber*> *cities = @{
@"Toronto": @(Toronto),
@"Vancouver": @(Vancouver),
};
return cities[self].integerValue;
}
@end
sample usage:
示例用法:
NSString *myCity = @"Vancouver";
City enumValue = [myCity cityEnumFromString];
NSLog(@"Expect 1, Actual %@", @(enumValue));
回答by Tom Jefferys
You could also put the values in an array.
您也可以将值放入数组中。
NSArray *carColorsArray = @[@"red", @"blue", @"white"];
You can then use indexOfObject to get the index of a particular string.
然后您可以使用 indexOfObject 来获取特定字符串的索引。
car_colors carColor = [carColorsArray indexOfObject:@"blue"] + 1;
回答by andyvn22
There are a lot of great answers to this here: Converting between C enum and XML
这里有很多很好的答案:Converting between C enum and XML
They are basically the same as Abizern's, but are a little cleaner and easier to work with if your app does this string-to-enum conversion a lot. There are solutions which keep the string and enum definitions together, and ways to make the conversions each a single, easy-to-read line of code.
它们与 Abisern 的基本相同,但如果您的应用程序经常执行这种字符串到枚举的转换,则它们会更简洁且更易于使用。有一些解决方案可以将字符串和枚举定义保持在一起,以及使每个转换成为单个易于阅读的代码行的方法。
回答by uranpro
// ...
typedef enum {
One = 0,
Two,
Three
} GFN;
// ...
#define kGFNPrefix @"GFNEnum_"
// ...
+ (NSString *)gfnToStr:(GFN)gfn {
return [NSString stringWithFormat:@"%@%d", kGFNPrefix, gfn];
}
+ (GFN)gfnFromStr:(NSString *)str {
NSString *gfnStr = [str stringByReplacingOccurrencesOfString:kGFNPrefix withString:@""];
return [gfnStr intValue];
}
// ...
My choice =)
我的选择 =)
回答by Hectoret
I found the solution:
我找到了解决方案:
if ([car_color isEqualToString:@"1"])
return red;
if ([tipo_pdi isEqualToString:@"2"])
return blue;
if ([tipo_pdi isEqualToString:@"3"])
return white;
But I don't like this 'if' style, what if I had a thousand colors? Isn't there a more automatic solution?
但我不喜欢这种“如果”风格,如果我有一千种颜色怎么办?没有更自动的解决方案吗?

