What's the cleanest way to store an enum in XML and read it back out again? Say I've got:
enum ETObjectType {ETNormalObjectType, ETRareObjectType, ETEssentialObjectType};
...and I want to take a variable, enum ETObjectType objectType = ETNormalObjectType;
, and convert it to XML that looks like this: <objectType>ETNormalObjectType</objectType>
.
Currently what I'm doing is something like this:
NSString* const ETObjectTypeAsString[] = {@"ETNormalObjectType",@"ETRareObjectType",@"ETEssentialObjectType"};
[anXMLElement addChild:[NSXMLElement elementWithName:@"objectType" stringValue:ETObjectTypeAsString[objectType]]];
...but that's not entirely ideal; I'm not happy about updating both lists every time I change my enum. But it's acceptable. Much, much worse is reading XML back in, for which I am currently doing this:
if ([[[anXMLElement childNamed:@"objectType"] stringValue] isEqualToString:@"ETRareObjectType"])
{
[self initObjectType:ETRareObjectType];
}
else if ([[[anXMLElement childNamed:@"objectType"] stringValue] isEqualToString:@"ETEssentialObjectType"])
{
[self initObjectType:ETEssentialObjectType];
}
else
{
[self initObjectType:ETNormalObjectType];
}
Yuck! This disgusts me. There's got to be a cleaner way to read, at least, or perhaps a unified way to read and write?
I'm using Obj-C and Cocoa, but I wouldn't mind some pure C functions. I'd even use preprocessor stuff, if it's the only way.