简体   繁体   English

检查是否存在目标c enum

[英]Check if a objective c enum exists

I have predefined enum for buttons IDs: 我为按钮ID预定了枚举:

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. 我需要找出我收到的ID是否在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. 不应该单独使用enum ,而应该尝试使用struct ,这里@Richard的回答将帮助您如何做到这一点。

Change enum values at runtime? 在运行时更改枚举值?

https://stackoverflow.com/a/10305425/1083859 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. 在上面的链接中,他解释了如何使用struct的动态enum值,并且您可以iterate值以找出。 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. enum不是对象,它只是编译器在构建时理解的整数。 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. 如果您没有预先定义枚举值,则它们将从0开始并增加1。 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 在.h

typedef enum
{
    407,
    999,
    408,
    409,
} TOP_MENU_BUTTON_TYPE;

@interface CheckoutController : UIViewController{
TOP_MENU_BUTTON_TYPE type;
}  

In .m 在.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 || 关于使用switch或一堆||答案 in if are correct, but… if是正确的,但......

If you have big enums (enum with a lot of values) you can make this simplier. 如果你有大的枚举 (枚举有很多值)你可以使这更简单。 Also Cocoa uses this trick. Cocoa也使用这个技巧。

Your enum values must be incremented by one . 您的枚举值必须加1
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) ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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