5

If I have an enum:

typedef enum {
  SOMETHING,
} MyEnum

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

1 Answer 1

8

There isn't really a clean way to do this in Objective-C (or C, for that matter).

You're going to have to map the enum values to their string counterparts. There are a number of ways you can do this: (1) A dictionary, as you mentioned. (2) A switch statement. (3) An array of string values where each index maps to a the corresponding enum value.

Sign up to request clarification or add additional context in comments.

1 Comment

FYI... switch statements in Objective-C can only operate on integer types. You would have to use an if...else if... construct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.