6

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)
Luda
  • 7,282
  • 12
  • 79
  • 139
  • possible duplicate of [Find int value in enum](http://stackoverflow.com/questions/4329214/find-int-value-in-enum) – Simon Whitaker Feb 13 '13 at 12:39
  • Unfortunately this question doesn't has an satisfactory answer. No dynamic function. All the suggestions use hard coded values of the enum. – Luda Feb 13 '13 at 13:08
  • Skip the 999 so that all values are contiguous with none skipped. Then add "buttonEnumMax" at the end. Check for a number between zero and "buttonEnumMax". – Hot Licks Feb 13 '13 at 13:16
  • The IDs are random numbers for me – Luda Feb 13 '13 at 13:28
  • 2
    Then check if the value is random. ;) – Hot Licks Feb 13 '13 at 20:44
  • Create a hashset or bitset that enumerates all the valid values. Check if the set contains the supplied value. – Hot Licks Feb 13 '13 at 20:45
  • (Since you indicate that new items can be added to your enumeration fairly dynamically, it must be the case that you have some sort of table to determine what action to take for a specific enumeration value. Check whether the supplied value is in that table.) – Hot Licks Feb 13 '13 at 20:47
  • What table? What hashset? I have an enum, that is constantly changing. And I want to have the option to check if the enum has value x. How can I do that? – Luda Feb 14 '13 at 08:34
  • Does it have to be an enum? As several others have pointed out to you, an enum with non-sequential values is not something you can dynamically iterate at run time. Where do you use the enum? – jrturton Feb 17 '13 at 09:08
  • That is exactly what I am looking for: dynamically iterate on enum – Luda Feb 17 '13 at 11:31
  • That is not possible, because **enums are harcoded values**. This is not Java… – Tricertops Feb 17 '13 at 20:04
  • In Java they are not hard coded? – Luda Feb 18 '13 at 07:58
  • In Java _enum is a class_ and _its values are objects_ and I guess they can be inspected during runtime using reflection. Here, enum is just a set of numbers that are given names. The names (nor the typedef'd type) don't exist during runtime, so they can not be accessed dynamically. – Tricertops Feb 18 '13 at 08:11
  • Experienced programmers often forget that, when a beginner asks something the solution is often **extremely simple**. The only thing anyone had to answer here was "oh you're looking for this thing called a dictionary." – Fattie Apr 17 '14 at 08:15
  • Next we'll have someone wanting to know how to use #defines as variables, etc :) – Fattie Apr 17 '14 at 08:18

7 Answers7

2

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.

Tricertops
  • 8,492
  • 1
  • 39
  • 41
  • @Luda No matter how big you make the bounty, this is going to be the answer. – rob mayoff Feb 20 '13 at 07:46
  • What do you think about the answer provided by R.A? – Luda Feb 20 '13 at 08:22
  • 1
    It' ugly. 1. It's based on declaring `struct` for each `enum` so it's no longer an **enum** 2. If you make such struct for each enum, you will use **not just the type, but also harcode all the values** duplicitly. I think you wanted to avoid this. Otherwise you can use some other answers here as well. 3. You told us you just get the enum declared by some another programmer. If you can modify the code and change it to `struct`, **you can also change the values**, or do it in **some really Objective-C way**. For example `NSArray` or `NSDictionary` like the answer provided here. – Tricertops Feb 21 '13 at 07:03
  • Right. The situation is straightforward. the OP is a beginner programmer and has not yet heard of the wonders of NSDictionary. Fortunately, it's incredibly easy to use NSDictionary. (Much easier than enum, bizarrely!) Here's a perfect beginner's post on dictionary. http://stackoverflow.com/a/1760384/294884 – Fattie Apr 17 '14 at 08:13
1

You can simply do this:

int validValue = button1ID | button2ID | button3ID | button4ID;
if (validValue & id)
    // Valid enum value
nomann
  • 2,257
  • 2
  • 21
  • 24
  • Please see bounty description: In your answer do not use hard coded values of the enum, just its type. – Luda Feb 19 '13 at 19:42
  • IMO that is ok only with small example code but if there are hundreds or thousands values defined it will be too hard to maintain without making a mistake. Though from other side that solution is fine if combined with automatic code generator. – Andrew Jul 26 '22 at 06:37
1

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.

Community
  • 1
  • 1
Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81
  • If my explanation sucks, then go on with the link. – Dinesh Raja Feb 20 '13 at 07:37
  • It look like the most correct answer. But there is something I am doing wrong. Look: In .h: http://i49.tinypic.com/2j5ctxd.png In .m:http://i48.tinypic.com/p99pf.png – Luda Feb 20 '13 at 08:17
  • I got it.. In your first image , just see that you have typedef keyword before the struct. Remove that and try. that will do the trick – Dinesh Raja Feb 20 '13 at 08:57
  • It worked. I really hoped that I could find answer using enum. But that was the closesed – Luda Feb 20 '13 at 09:57
0

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

If you aren't pre-defining the values of your enums, they will start at 0 and increase by one. This lets you compare a value to see if it's <= your last element.

mah
  • 39,056
  • 9
  • 76
  • 93
  • As you can see in the example, my values are predefined. So what can I do? – Luda Feb 13 '13 at 12:32
  • _Because of this, you would need to provide low level code to make your check_. I.e., `if ((value != button1ID) && (value != button2ID) && ...) printf("the value is not in my enum");`. Perhaps you have some other alternative, such as adding the actual `id` of each button (not this numeric value, the obj-c assigned id) to an `NSDictionary` along with your numeric id (as an `NSNumber`), and then checking for the id being in the dictionary. – mah Feb 13 '13 at 12:37
  • I didn't understand the alternative, anyway it should be dinamic – Luda Feb 13 '13 at 12:47
  • Can you give a code example for this: Perhaps you have some other alternative, such as adding the actual id of each button (not this numeric value, the obj-c assigned id) to an NSDictionary along with your numeric id (as an NSNumber), and then checking for the id being in the dictionary. ? – Luda Feb 13 '13 at 12:59
  • The code is not that complicated, but you would be much better off writing it yourself so you will be sure to understand it. http://www.gnustep.it/nicola/Tutorials/BasicClasses/node28.html is an example of using a `NSMutableDictionary` (useful for dynamically modifying the dictionary at runtime), http://stackoverflow.com/questions/1760371/how-can-we-store-into-an-nsdictionary-what-is-the-difference-between-nsdictiona shows defining a static `NSDictionary`. You can use NSDictionary's `- (id)objectForKey:(id)aKey` method to determine if your numeric id is in the dictionary (as a NSNumber object) – mah Feb 13 '13 at 14:54
  • How can a dictionary help me in this case? – Luda Feb 13 '13 at 14:58
  • A dictionary is a map of keys (which would be NSNumber's holding your enum values) to objects; they are a simple way to map your enums to the buttons they represent. Another possibility is to simply use an NSArray of your enum values; see http://www.gnustep.it/nicola/Tutorials/BasicClasses/node13.html – mah Feb 13 '13 at 16:05
0

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 %d a TOP_MENU_BUTTON_TYPE? result: %d", test, [self isDefined:test]);
    test = 2;
    NSLog(@"is %d a TOP_MENU_BUTTON_TYPE? result: %d", test, [self isDefined:test]);

so:

if ([self isDefined:test]){
    // OK, test is defined in TOP_MENU_BUTTON_TYPE
} 
meronix
  • 6,175
  • 1
  • 23
  • 36
0

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;
    }
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
0

Answers about using switch or bunch of || in if are correct, but…

If you have big enums (enum with a lot of values) you can make this simplier. Also Cocoa uses this trick.

Your enum values must be incremented by one.
Then add to enum two additional values:

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 are comparing, you just need to do:

if (buttonID >= buttonIDMin && buttonID <= buttonIDMax) ...
Tricertops
  • 8,492
  • 1
  • 39
  • 41
  • Hi iMartin. The values are not ordered – Luda Feb 17 '13 at 11:28
  • I see, that in your example they are not, but in general this is quite easy way to check if the value is in the enum. – Tricertops Feb 17 '13 at 20:03
  • Luda, why don't you make the values ordered? There doesn't seem to be a good reason that button2ID should be 999, and iMartin's answer is simple and works great. – Ron Feb 18 '13 at 03:16
  • 1
    Because it is not me that provides the values – Luda Feb 18 '13 at 07:59