如何在目标c中使用枚举

时间:2023-01-28 00:18:41

I am abit stuck on how I use enum's in my project, I have set up the the enum object but I would like to know how to use it.

我坚持如何在我的项目中使用枚举,我已经设置了枚举对象,但我想知道如何使用它。

This is what I have done so far

这是我到目前为止所做的

//.h

   typedef enum {
      ktUnknown=0, ktSingleSided=1, ktDoubleSided=2, ktTripleSingleSided=3
    } TICKType;
    //..
    TICKType Type;
    //..
    @property (assign) TICKType Type;

Now I would like to know how to check if an integer equals one of those enum types in an if statement.

现在我想知道如何检查整数是否等于if语句中的那些枚举类型之一。

this is kinda what im doing obviously not working

这有点像我做的显然不起作用

if (myobj.objsval == Type.ktSingleSided) {

}

but unfortunately this is not working. any help figuring this out would be greatly appreciated.

但不幸的是,这不起作用。任何帮助搞清楚这一点将不胜感激。

2 个解决方案

#1


2  

 typedef enum {
      ktUnknown=0, ktSingleSided=1, ktDoubleSided=2, ktTripleSingleSided=3
    } TICKType;

enum can be compared to integer value only if myobj.objsval is a integer property

仅当myobj.objsval是整数属性时,才能将enum与整数值进行比较

if (myobj.objsval == ktSingleSided) the this condition will be satisfied

#2


1  

in OC use enum like this

在OC中使用这样的枚举

if (myobj.objsval == ktSingleSided){
}

you don't need to type declared here.

你不需要在这里声明类型。

#1


2  

 typedef enum {
      ktUnknown=0, ktSingleSided=1, ktDoubleSided=2, ktTripleSingleSided=3
    } TICKType;

enum can be compared to integer value only if myobj.objsval is a integer property

仅当myobj.objsval是整数属性时,才能将enum与整数值进行比较

if (myobj.objsval == ktSingleSided) the this condition will be satisfied

#2


1  

in OC use enum like this

在OC中使用这样的枚举

if (myobj.objsval == ktSingleSided){
}

you don't need to type declared here.

你不需要在这里声明类型。