Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 308635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:40:07+00:00 2026-05-12T07:40:07+00:00

What’s the cleanest way to store an enum in XML and read it back

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:40:07+00:00Added an answer on May 12, 2026 at 7:40 am

    I haven’t found a better way than duplicating the enum in a string. However, I do it slightly differently, namely:

    typedef enum {
        kManipulateWindowTargetFrontWindow,
        kManipulateWindowTargetNamedWindow, 
        kManipulateWindowTargetWindowNameContaining, 
        kManipulateWindowTargetDEFAULT = kManipulateWindowTargetFrontWindow, 
    } ManipulateWindowTargetType;
    #define kManipulateWindowTargetTypeNamesArray @"FrontWindow", @"NamedWindow", @"WindowNameContaining", nil
    

    then in the implementation:

    static NSArray* kManipulateWindowTargetTypeArray = [[NSArray alloc] initWithObjects: kManipulateWindowTargetTypeNamesArray];
    
    NSString* ManipulateWindowTargetTypeToString( ManipulateWindowTargetType mwtt )
    {
        return [kManipulateWindowTargetTypeArray objectAtIndex:mwtt];
    }
    
    ManipulateWindowTargetType ManipulateWindowTargetTypeFromString( NSString* s )
    {
        NSUInteger n = [kManipulateWindowTargetTypeArray indexOfObject:s];
        check( n != NSNotFound );
        if ( n == NSNotFound ) {
            n = kManipulateWindowTargetDEFAULT;
        }
        return (ManipulateWindowTargetType) n;
    }
    

    The reason I use the #define is to avoid declaring the array in the header file, but it would be insane to separate the definition of the enum from the definition of the sequence of strings, so this is the best compromise I’ve found.

    Since the code is boilerplate, you can actually make them a category on NSArray.

    @interface NSArray (XMLExtensions)
    
    - (NSString*) stringWithEnum: (NSUInteger) e;
    - (NSUInteger) enumFromString: (NSString*) s default: (NSUInteger) def;
    - (NSUInteger) enumFromString: (NSString*) s;
    
    @end
    
    @implementation NSArray (XMLExtensions)
    
    - (NSString*) stringWithEnum: (NSUInteger) e;
    {
        return [self objectAtIndex:e];
    }
    
    - (NSUInteger) enumFromString: (NSString*) s default: (NSUInteger) def;
    {
        NSUInteger n = [self indexOfObject:s];
        check( n != NSNotFound );
        if ( n == NSNotFound ) {
            n = def;
        }
        return n;
    }
    
    - (NSUInteger) enumFromString: (NSString*) s;
    {
        return [self enumFromString:s default:0];
    }
    
    
    @end
    

    and then:

    NSLog( @"s is %@", [kManipulateWindowTargetTypeArray stringWithEnum:kManipulateWindowTargetNamedWindow] );
    ManipulateWindowTargetType mwtt = (ManipulateWindowTargetType)[kManipulateWindowTargetTypeArray enumFromString:@"WindowNameContaining" default:kManipulateWindowTargetDEFAULT];
    NSLog( @"e is %d", mwtt );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.