views:

4018

answers:

5

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;

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 *value = [[NSString alloc] initWithString:@"1"];
myCar = [value intValue]; // <-- doesn't work

any idea? thanks!

+2  A: 

You could also put the values in an array.

NSArray *carColorsArray = [NSArray arrayWithObjects:@"red", @"blue", @"white", nil];

You can then use indexOfObject to get the index of a particular string.

car_colors carColor = [carColorsArray indexOfObject:@"blue"] + 1;
Tom
+4  A: 

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).

// Create the dictionary of colour strings and numbers.
NSArray *keyArray = [NSArray arrayWithObjects:@"Red", @"Blue", @"White", nil];
NSArray *objectArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],
                                                 [NSNumber numberWithInt:2],
                                                 [NSNumber numberWithInt:3],
                                                 nil];

NSDictionary *carColourDictionary = [NSDictionary dictionaryWithObjects:objectArray 
                                                  forKeys:keyArray];

// 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 objectForKey:[object colourAsString]];
Abizern
A: 

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?

Hectoret
A dictionary - As I said in my answer.
Abizern
I won't vote you down, but what you are doing is just wrong. The answer by @Tom is much better than doing this.
Brock Woolf
+1  A: 

here's an implementation using NSDictionary and the existing enum

in .h file:

typedef enum city{
    Toronto         = 0,
    Vancouver       = 1
} City;

@interface NSString (EnumParser)
- (City)CityEnumFromString; 
@end

in .m file:

@implementation NSString (EnumParser)
- (City)CityEnumFromString{
    NSDictionary *Cities = [NSDictionary dictionaryWithObjectsAndKeys:
          [NSNumber numberWithInteger:Toronto], @"Toronto",
          [NSNumber numberWithInteger:Vancouver], @"Vancouver",
          nil
          ];
    return (City)[[Cities objectForKey:self] intValue];
}
@end

sample usage:

NSString *myCity = [NSString stringWithFormat:@"Vancouver"];
City enumValue = [myCity CityEnumFromString];
NSLog([NSString stringWithFormat:@"Expect 1, Actual %@",[NSNumber numberWithInt:(int)enumValue]]);
A: 

There are a lot of great answers to this here: http://stackoverflow.com/questions/1242914/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.

andyvn22