Check if object c-enum exists

I have a predefined enumeration for button identifiers:

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

I need to find out if the ID that I get matches the listing. How can i do this? Sort of:

 if(id in TOP_MENU_BUTTON_TYPE) 
+6
source share
7 answers

If I understand your question clearly, then it will be useful for you.

Instead of using only enum , you should try this with struct , and here is the answer from @Richard will help you how to do this.

Change enumeration values ​​at runtime?

fooobar.com/questions/914069 / ...

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

+1
source

There is no way to dynamically iterate over an enumeration. Enumerations are a static function ; they do not exist at run time. At run time, they are simply simple integers (of a certain size) and values.

This is not possible with this requirement indicated in the award:

In your answer, do not use hardcoded enum values, just its type.


Other answers show that you almost completely use to do this statically .

+2
source

You can simply do this:

 int validValue = button1ID | button2ID | button3ID | button4ID; if (validValue & id) // Valid enum value 
+1
source

An enum not an object, it is just an integer that the compiler understands at build time. Because of this, you will need to provide a low level code to do your check.

If you do not predefine the values ​​of your enumerations, they start at 0 and increase by one. This allows you to compare the value to see if it is <= the last element.

0
source

try this method:

 -(BOOL)isDefined:(TOP_MENU_BUTTON_TYPE)type{ BOOL isDefined; switch (type) { case button1ID: case button2ID: case button3ID: case button4ID: isDefined = TRUE; break; default: isDefined = FALSE; break; } return isDefined; } //(...) TOP_MENU_BUTTON_TYPE test; test = 407; NSLog(@"is %da TOP_MENU_BUTTON_TYPE? result: %d", test, [self isDefined:test]); test = 2; NSLog(@"is %da TOP_MENU_BUTTON_TYPE? result: %d", test, [self isDefined:test]); 

So:

 if ([self isDefined:test]){ // OK, test is defined in TOP_MENU_BUTTON_TYPE } 
0
source

in .h

 typedef enum { 407, 999, 408, 409, } TOP_MENU_BUTTON_TYPE; @interface CheckoutController : UIViewController{ TOP_MENU_BUTTON_TYPE type; } 

In .m

 switch (status) { case 407: //Your Task break; case 999: //Your Task break; case 408: //Your Task break; case 409: //Your Task break; } 
0
source

Answers about using switch or bundle || in if are true, but ...

If you have large enumerations (an enumeration with a large number of values), you can make this simpler. Cocoa also uses this trick.

Your listing values ​​should be increased by one .
Then add two additional values ​​for listing:

 typedef enum { buttonIDMin = 407, // Lowest value button1ID = 407, button2ID = 408, // Incremented by ONE button3ID = 409, button4ID = 410, buttonIDMax = 410, // Highest value } TOP_MENU_BUTTON_TYPE; 

When you compare, you just need to do:

 if (buttonID >= buttonIDMin && buttonID <= buttonIDMax) ... 
0
source

All Articles