简体   繁体   中英

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)

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 .

You can simply do this:

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

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.

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.

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
} 

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;
    }

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) ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM