0

Does anyone know how to (or if) you can convert a string to an enumerated type in the objective-c/cocoa/iphone environment?

For example:

If I have an XML document with the element:

<dog breed="GermanShepard">

I would like to be able to read the "breed" attribute into an NSString and then convert it into a type "Dogs" as defined here:

typedef enum 
{   
Poodle,
GoldenRetriever,
GermanShepard,
Collie
} Dogs;

Dogs myDog = <string that came from the breed attr in the XML>

Any assistance is appreciated...

Also, I just want to clarify that I AM able to retrieve the attr value from the XML (this is pretty straightforward). However, my issue is that once I have an NSString, I am unable to convert it to an enumerated type (ie. the Dog type in the example). I only included the XML example above as a means of illustrating why I would want to do such a thing.

g-dog
  • 410
  • 1
  • 5
  • 14
  • A similar question was asked [here](https://stackoverflow.com/questions/104339/objective-c-switch-using-objects), and I think that many of the generic Cocoa-based answers given there might be useful in your case. – Brad Larson Mar 27 '09 at 13:05

4 Answers4

3

There's no way to do this out of the box. You'll need to make a table that allows you to associate the strings with the appropriate integer values at runtime.

Chuck
  • 234,037
  • 30
  • 302
  • 389
2

I'm fairly sure that the enum values are replaced with integers at compile time, so you'll either have to test individually for matches with strings, or have an array of your enum values that you call indexOfObject: on.

cobbal
  • 69,903
  • 20
  • 143
  • 156
1

you must put enum values into NSDictionary:

 NSDictionary *dogs = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithInt:Poodle], @"Poodle",
                          [NSNumber numberWithInt:GoldenRetriever],   @"GoldenRetriever",
                          //.....
                          //.....
                          nil];

somewhere in parsing class when you collect parsing values put it into another NSDictionary

 dictOfParsedData = [NSDictionary dictionaryWithObjectsAndKeys:
                     //.....
                     //.....
                     [dogs valueForKey:[attributeDict objectForKey:@"breed"]], @"breed",
                     //.....
                     nil];

method that returns element of enum type

-(NSInteger)valForGlobalKey:(NSString *)_key andSubKey:(NSString *)_key2{    
    return [[[dictOfParsedData valueForKey:_key] valueForKey:_key2] intValue];
}

somewhere in class which contains dogs

#import "ParsingClass.h" 
@interface ClassOfDogs : NSObject

implementation:

ParsingClass *parsingClassInst = [[ParsingClass alloc] initWithXMLFile:@"Dogs.xml"];    
Dogs myDog = [parsingClassInst valForGlobalKey:@"dog" andSubKey:@"breed"];
Stan
  • 122
  • 4
0

If you use NSXMLParser, this will be contained within the xml attributes passed to theparser:didStartElement:namespaceURI:qualifiedName:attributes: delegate method. when it is called for the dog element.

The attributes are held in a NSArray keyed with the attribute name. In your case "breed". So, you access the string containing the dog's name using:

NSString* breedName = [attributesDictionary objectForKey:@"breed"];

Then you have a string you can compare (using NSString's comparisons) to your dog names.

You could get slightly neater code by making your own NSDisctionary of NSNumbers containing your enum values keyed by the textual dognames.

Rog
  • 17,070
  • 9
  • 50
  • 73
  • 1
    In general, looking up a value from an NSDictionary is much faster than comparing it with each string in a list in sequence - especially if the list (of possible dog names) is long. A dictionary lookup is essentially done in constant time, while comparing to each string is O(n), with n the number of possible strings. – fishinear Nov 14 '12 at 12:01