Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a objective c enum exists

I have predefined enum for buttons IDs:

typedef enum
{
    button1ID = 407,
    button2ID = 999,
    button3ID = 408,
    button4ID = 409,
} TOP_MENU_BUTTON_TYPE;

I need to find out if the ID I recieve is defiened in the enum. How can I do that? Something like:

if(id in TOP_MENU_BUTTON_TYPE)
like image 891
Luda Avatar asked Oct 05 '22 01:10

Luda


2 Answers

There is no way to dynamically iterate an enum. Enums are static feature, they don't exist during runtime. In runtime they are just plain integers (of some size) and values.

It's not possible with this requirement you stated in bounty:

In your answer do not use hard coded values of the enum, just its type.


The other answers show you pretty much all ways to do it statically.

like image 81
Tricertops Avatar answered Oct 12 '22 15:10

Tricertops


If I understand your question clearly, then this would be helpful to you..

Instead of using enum alone, you should try that with struct and here it is an answer by @Richard will help you how to do that.

Change enum values at runtime?

https://stackoverflow.com/a/10305425/1083859

In the above link, he explains how to use a dynamic enum values with struct and also you can iterate the values to find out. I think you will get an idea.

like image 39
Dinesh Raja Avatar answered Oct 12 '22 13:10

Dinesh Raja