如何在Objective-C中定义和使用ENUM ?

时间:2022-09-01 09:32:54

I declared an enum in my implementation file as shown below, and declared a variable of that type in my interface as PlayerState thePlayerState; and used the variable in my methods. But I am getting errors stating that it is undeclared. How do I correctly declare and use a variable of type PlayerState in my methods?:

我在实现文件中声明了enum,如下所示,并在接口中声明了该类型的变量PlayerState thePlayerState;在我的方法中使用了变量。但是我有错误说它是未声明的。在我的方法中如何正确地声明和使用类型PlayerState变量?

In the .m file

在m文件

@implementation View1Controller

    typedef enum playerStateTypes
        {
            PLAYER_OFF,
            PLAYER_PLAYING,
            PLAYER_PAUSED
        } PlayerState;

in the .h file:

. h文件中:

@interface View1Controller : UIViewController {

    PlayerState thePlayerState;

in some method in .m file:

在.m文件的某些方法中:

-(void)doSomethin{

thePlayerState = PLAYER_OFF;

}

6 个解决方案

#1


107  

Your typedef needs to be in the header file (or some other file that's #imported into your header), because otherwise the compiler won't know what size to make the PlayerState ivar. Other than that, it looks ok to me.

你的typedef必须在头文件(或者是#导入到头文件中的其他文件)中,否则编译器就不知道要用什么大小来创建PlayerState ivar。除此之外,我觉得还行。

#2


193  

Apple provides a macro to help provide better code compatibility, including Swift. Using the macro looks like this.

苹果提供了一个宏来帮助提供更好的代码兼容性,包括Swift。使用宏就像这样。

typedef NS_ENUM(NSInteger, PlayerStateType) {
  PlayerStateOff,
  PlayerStatePlaying,
  PlayerStatePaused
};

Documented here

记录在这里

#3


25  

In the .h:

. h:

typedef enum {
    PlayerStateOff,
    PlayerStatePlaying,
    PlayerStatePaused
} PlayerState;

#4


19  

With current projects you may want to use the NS_ENUM() or NS_OPTIONS() macros.

对于当前项目,您可能希望使用NS_ENUM()或NS_OPTIONS()宏。

typedef NS_ENUM(NSUInteger, PlayerState) {
        PLAYER_OFF,
        PLAYER_PLAYING,
        PLAYER_PAUSED
    };

#5


16  

This is how Apple does it for classes like NSString:

这是苹果为NSString这样的类做的事情:

In the header file:

在头文件:

enum {
    PlayerStateOff,
    PlayerStatePlaying,
    PlayerStatePaused
};

typedef NSInteger PlayerState;

Refer to Coding Guidelines at http://developer.apple.com/

参见http://developer.apple.com/的编码指南。

#6


5  

I recommend using NS_OPTIONS or NS_ENUM. You can read more about it here: http://nshipster.com/ns_enum-ns_options/

我建议使用NS_OPTIONS或NS_ENUM。您可以在这里阅读更多信息:http://nshipster.com/ns_enum-ns_options/。

Here's an example from my own code using NS_OPTIONS, I have an utility that sets a sublayer (CALayer) on a UIView's layer to create a border.

这是我自己的代码中使用NS_OPTIONS的一个例子,我有一个实用程序,它在UIView的层上设置一个子类(CALayer)来创建一个边框。

The h. file:

h文件:

typedef NS_OPTIONS(NSUInteger, BSTCMBorder) {
    BSTCMBOrderNoBorder     = 0,
    BSTCMBorderTop          = 1 << 0,
    BSTCMBorderRight        = 1 << 1,
    BSTCMBorderBottom       = 1 << 2,
    BSTCMBOrderLeft         = 1 << 3
};

@interface BSTCMBorderUtility : NSObject

+ (void)setBorderOnView:(UIView *)view
                 border:(BSTCMBorder)border
                  width:(CGFloat)width
                  color:(UIColor *)color;

@end

The .m file:

m文件:

@implementation BSTCMBorderUtility

+ (void)setBorderOnView:(UIView *)view
                 border:(BSTCMBorder)border
                  width:(CGFloat)width
                  color:(UIColor *)color
{

    // Make a left border on the view
    if (border & BSTCMBOrderLeft) {

    }

    // Make a right border on the view
    if (border & BSTCMBorderRight) {

    }

    // Etc

}

@end

#1


107  

Your typedef needs to be in the header file (or some other file that's #imported into your header), because otherwise the compiler won't know what size to make the PlayerState ivar. Other than that, it looks ok to me.

你的typedef必须在头文件(或者是#导入到头文件中的其他文件)中,否则编译器就不知道要用什么大小来创建PlayerState ivar。除此之外,我觉得还行。

#2


193  

Apple provides a macro to help provide better code compatibility, including Swift. Using the macro looks like this.

苹果提供了一个宏来帮助提供更好的代码兼容性,包括Swift。使用宏就像这样。

typedef NS_ENUM(NSInteger, PlayerStateType) {
  PlayerStateOff,
  PlayerStatePlaying,
  PlayerStatePaused
};

Documented here

记录在这里

#3


25  

In the .h:

. h:

typedef enum {
    PlayerStateOff,
    PlayerStatePlaying,
    PlayerStatePaused
} PlayerState;

#4


19  

With current projects you may want to use the NS_ENUM() or NS_OPTIONS() macros.

对于当前项目,您可能希望使用NS_ENUM()或NS_OPTIONS()宏。

typedef NS_ENUM(NSUInteger, PlayerState) {
        PLAYER_OFF,
        PLAYER_PLAYING,
        PLAYER_PAUSED
    };

#5


16  

This is how Apple does it for classes like NSString:

这是苹果为NSString这样的类做的事情:

In the header file:

在头文件:

enum {
    PlayerStateOff,
    PlayerStatePlaying,
    PlayerStatePaused
};

typedef NSInteger PlayerState;

Refer to Coding Guidelines at http://developer.apple.com/

参见http://developer.apple.com/的编码指南。

#6


5  

I recommend using NS_OPTIONS or NS_ENUM. You can read more about it here: http://nshipster.com/ns_enum-ns_options/

我建议使用NS_OPTIONS或NS_ENUM。您可以在这里阅读更多信息:http://nshipster.com/ns_enum-ns_options/。

Here's an example from my own code using NS_OPTIONS, I have an utility that sets a sublayer (CALayer) on a UIView's layer to create a border.

这是我自己的代码中使用NS_OPTIONS的一个例子,我有一个实用程序,它在UIView的层上设置一个子类(CALayer)来创建一个边框。

The h. file:

h文件:

typedef NS_OPTIONS(NSUInteger, BSTCMBorder) {
    BSTCMBOrderNoBorder     = 0,
    BSTCMBorderTop          = 1 << 0,
    BSTCMBorderRight        = 1 << 1,
    BSTCMBorderBottom       = 1 << 2,
    BSTCMBOrderLeft         = 1 << 3
};

@interface BSTCMBorderUtility : NSObject

+ (void)setBorderOnView:(UIView *)view
                 border:(BSTCMBorder)border
                  width:(CGFloat)width
                  color:(UIColor *)color;

@end

The .m file:

m文件:

@implementation BSTCMBorderUtility

+ (void)setBorderOnView:(UIView *)view
                 border:(BSTCMBorder)border
                  width:(CGFloat)width
                  color:(UIColor *)color
{

    // Make a left border on the view
    if (border & BSTCMBOrderLeft) {

    }

    // Make a right border on the view
    if (border & BSTCMBorderRight) {

    }

    // Etc

}

@end