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, ETEssent
I echo Jon's solution, but you can use the dreaded X-macro to avoid repeating yourself at all. I don't know how to comment on Jon's answer with code formatting, so here it is as a new answer.
#define ETObjectTypeEntries \
ENTRY(ETNormalObjectType) \
ENTRY(ETRareObjectType) \
ENTRY(ETEssentialObjectType)
typedef enum ETObjectType {
#define ENTRY(objectType) objectType,
ETObjectTypeEntries
#undef ENTRY
} ETObjectType;
NSString *ETObjectTypesAsStrings[] = {
#define ENTRY(objectType) [objectType] = @"" # objectType,
ETObjectTypeEntries
#undef ENTRY
};
#define countof(array) (sizeof(array)/sizeof(array[0]))
NSString *ETStringFromObjectType(ETObjectType type) {
return ETObjectTypesAsStrings[type];
}
NSString *ETObjectTypeFromString(NSString *string) {
NSString *match = nil;
for(NSInteger idx = 0; !match && (idx < countof(ETObjectTypesAsStrings)); idx += 1) {
if ([string isEqualToString:ETObjectTypesAsStrings[idx]]) {
match = ETObjectTypesAsStrings[idx];
}
}
return match;
}