Goal C: Convert String to Enumeration

If I have an enumeration:

typedef enum { SOMETHING, } MyEnum 

and I have NSString "SOMETHING", is there a way that I can go directly from the string to the ENUM value? I understand that I can just make a dictionary for this, but I'm curious.

+4
source share
1 answer

There is really no clean way to do this in Objective-C (or C, for that matter).

You will need to match the enum values โ€‹โ€‹with your string counterparts. There are several ways to do this: (1) a dictionary, as you mentioned. (2) Switch operator. (3) An array of row values, where each index displays the corresponding enumeration value.

+7
source

All Articles