Objective-C:我应该在哪里以及如何声明枚举?

时间:2021-02-26 20:57:09

Good day, friends. I'm newbie in Objective-C. I'm wanting to use enum in my class and make it public. I've understand how to declare enums (http://*.com/questions/1662183/using-enum-in-objective-c), but I don't understand where should I declare them.

美好的一天,朋友们。我是Objective-C的新手。我想在课堂上使用枚举并将其公之于众。我已经了解如何声明枚举(http://*.com/questions/1662183/using-enum-in-objective-c),但我不明白我应该在哪里声明它们。

I've tried:

@interface MyFirstClass : NSObject {
typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes;
}

or:

@interface MyFirstClass : NSObject {
@public
   typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes;
}

But compiler throws error: "expected specifier-qualifier-list before typedef".

但是编译器抛出错误:“在typedef之前预期的说明符 - 限定符列表”。

What's wrong?

3 个解决方案

#1


10  

.h

typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes;

@interface MyFirstClass : NSObject {

 MyTypes type;

 }

.m file

   type=VALUE_A;

#2


7  

Outside of the @interface declaration.

在@interface声明之外。

typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes;

@interface MyFirstClass : NSObject {
}

@end

#3


2  

You can create a header file (*.h) and do following to match your enum variable.

您可以创建一个头文件(* .h)并执行以下操作以匹配您的枚举变量。

//  EnumConstants.h


#ifndef EnumConstants_h
#define EnumConstants_h

typedef enum {
    VEHICLE,
    USERNAME
} EDIT_TYPE;

typedef enum {
    HIGH_FLOW,
    STANDARD_FLOW
} FLOW_TYPE;


#endif

Uses:

#import "EnumConstants.h"

UISwitch *onOffSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(self.tableview.frame.size.width-75, 26, 0, 0)];
onOffSwitch.tag =STANDARD_FLOW;

#1


10  

.h

typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes;

@interface MyFirstClass : NSObject {

 MyTypes type;

 }

.m file

   type=VALUE_A;

#2


7  

Outside of the @interface declaration.

在@interface声明之外。

typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes;

@interface MyFirstClass : NSObject {
}

@end

#3


2  

You can create a header file (*.h) and do following to match your enum variable.

您可以创建一个头文件(* .h)并执行以下操作以匹配您的枚举变量。

//  EnumConstants.h


#ifndef EnumConstants_h
#define EnumConstants_h

typedef enum {
    VEHICLE,
    USERNAME
} EDIT_TYPE;

typedef enum {
    HIGH_FLOW,
    STANDARD_FLOW
} FLOW_TYPE;


#endif

Uses:

#import "EnumConstants.h"

UISwitch *onOffSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(self.tableview.frame.size.width-75, 26, 0, 0)];
onOffSwitch.tag =STANDARD_FLOW;