Converting between C enum and XML

前端 未结 5 372
遇见更好的自我
遇见更好的自我 2020-12-04 20:21

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         


        
5条回答
  •  臣服心动
    2020-12-04 21:03

    I'm experimenting with this solution -

    static NSString *stepTypeEnum[kStepTypeCount] = {@"one",@"two",@"three",@"four"};
    
    int enumFromStrings(NSString*findString,NSString *strings[],int enumMax){
        for (int i=0;i

    I like it because it checks the length of the string array at compile time, and the enumFromStrings function is generic and reusable. You call it like so:

    -(void)setType:(NSString*)typeString{
        type = enumFromStrings(typeString,stepTypeEnum,kStepTypeCount);
    }
    

提交回复
热议问题