enum Values to NSString (iOS)

前端 未结 16 1895
悲&欢浪女
悲&欢浪女 2020-12-04 07:57

I have an enum holding several values:

enum {value1, value2, value3} myValue;

In a certain point in my app, I wish to check which value of

相关标签:
16条回答
  • 2020-12-04 08:20

    In some cases when you need to convert enum -> NSString and NSString -> enum it might be simpler to use a typedef and #define (or const NSStrings) instead of enum:

    typedef NSString *        ImageType;
    #define ImageTypeJpg      @"JPG"
    #define ImageTypePng      @"PNG"
    #define ImageTypeGif      @"GIF"
    

    and then just operate with "named" strings as with any other NSString:

    @interface MyData : NSObject
    @property (copy, nonatomic) ImageType imageType;
    @end
    
    @implementation MyData
    - (void)doSomething {
        //...
        self.imageType = ImageTypePng;
        //...
        if ([self.imageType isEqualToString:ImageTypeJpg]) {
            //...
        }
    }
    @end
    
    0 讨论(0)
  • 2020-12-04 08:20

    This is an old question, but if you have a non contiguous enum use a dictionary literal instead of an array:

    typedef enum {
        value1 = 0,
        value2 = 1,
        value3 = 2,
    
        // beyond value3
        value1000 = 1000,
        value1001
    } MyType;
    
    #define NSStringFromMyType( value ) \
    ( \
        @{ \
            @( value1 )    : @"value1", \
            @( value2 )    : @"value2", \
            @( value3 )    : @"value3", \
            @( value1000 ) : @"value1000", \
            @( value1001 ) : @"value1001", \
        } \
        [ @( value ) ] \
    )
    
    0 讨论(0)
  • 2020-12-04 08:25

    I found this website (from which the example below is taken) which provides an elegant solution to this problem. The original posting though comes from this StackOverflow answer.

    // Place this in your .h file, outside the @interface block
    typedef enum {
        JPG,
        PNG,
        GIF,
        PVR
    } kImageType;
    #define kImageTypeArray @"JPEG", @"PNG", @"GIF", @"PowerVR", nil
    
    ...
    
    // Place this in the .m file, inside the @implementation block
    // A method to convert an enum to string
    -(NSString*) imageTypeEnumToString:(kImageType)enumVal
    {
        NSArray *imageTypeArray = [[NSArray alloc] initWithObjects:kImageTypeArray];
        return [imageTypeArray objectAtIndex:enumVal];
    }
    
    // A method to retrieve the int value from the NSArray of NSStrings
    -(kImageType) imageTypeStringToEnum:(NSString*)strVal
    {
        NSArray *imageTypeArray = [[NSArray alloc] initWithObjects:kImageTypeArray];
        NSUInteger n = [imageTypeArray indexOfObject:strVal];
        if(n < 1) n = JPG;
        return (kImageType) n;
    }
    
    0 讨论(0)
  • 2020-12-04 08:25

    This is similar to the "X" macro post by pixel. thanks for the link to http://en.wikipedia.org/wiki/X_Macro

    Code generated in macros can be tricky and hard to debug. Instead, generate a table that is used by "normal" code. I find that many people object to having macros generate code, and may be one reason the technique of "X Macros" as presented in the wiki is not widely adopted.

    By generating a table, you still need only edit one place to extend the list, and since you can't "step through" a table in the debugger, this removes the objection from many people about multi-lines of code buried in macros.

    //------------------------------------------------------------------------------
    // enum to string example
    #define FOR_EACH_GENDER(tbd) \
            tbd(GENDER_MALE) \
            tbd(GENDER_FEMALE) \
            tbd(GENDER_INTERSEX) \
    
    #define ONE_GENDER_ENUM(name) name,
    enum
    {
        FOR_EACH_GENDER(ONE_GENDER_ENUM)
        MAX_GENDER
    };
    
    #define ONE_GENDER(name) #name,
    static const char *enumGENDER_TO_STRING[] = 
    {
        FOR_EACH_GENDER(ONE_GENDER)
    };
    
    // access string name with enumGENDER_TO_STRING[value]
    // or, to be safe converting from a untrustworthy caller
    static const char *enumGenderToString(unsigned int value)
    {
        if (value < MAX_GENDER)
        {
            return enumGENDER_TO_STRING[value];
        }
        return NULL;
    }
    
    static void printAllGenders(void)
    {
        for (int ii = 0;  ii < MAX_GENDER;  ii++)
        {
            printf("%d) gender %s\n", ii, enumGENDER_TO_STRING[ii]);
        }
    }
    
    //------------------------------------------------------------------------------
    // you can assign an arbitrary value and/or information to each enum,
    #define FOR_EACH_PERSON(tbd) \
            tbd(2, PERSON_FRED,     "Fred",     "Weasley", GENDER_MALE,   12) \
            tbd(4, PERSON_GEORGE,   "George",   "Weasley", GENDER_MALE,   12) \
            tbd(6, PERSON_HARRY,    "Harry",    "Potter",  GENDER_MALE,   10) \
            tbd(8, PERSON_HERMIONE, "Hermione", "Granger", GENDER_FEMALE, 10) \
    
    #define ONE_PERSON_ENUM(value, ename, first, last, gender, age) ename = value,
    enum
    {
        FOR_EACH_PERSON(ONE_PERSON_ENUM)
    };
    
    typedef struct PersonInfoRec
    {
        int value;
        const char *ename;
        const char *first;
        const char *last;
        int gender;
        int age;
    } PersonInfo;
    
    #define ONE_PERSON_INFO(value, ename, first, last, gender, age) \
                         { ename, #ename, first, last, gender, age },
    static const PersonInfo personInfo[] = 
    {
        FOR_EACH_PERSON(ONE_PERSON_INFO)
        { 0, NULL, NULL, NULL, 0, 0 }
    };
    // note: if the enum values are not sequential, you need another way to lookup
    // the information besides personInfo[ENUM_NAME]
    
    static void printAllPersons(void)
    {
        for (int ii = 0;  ;  ii++)
        {
            const PersonInfo *pPI = &personInfo[ii];
            if (!pPI->ename)
            {
                break;
            }
            printf("%d) enum %-15s  %8s %-8s %13s %2d\n",
                pPI->value, pPI->ename, pPI->first, pPI->last,
                enumGenderToString(pPI->gender), pPI->age);
        }
    }
    
    0 讨论(0)
提交回复
热议问题