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

Rex*_*ids 181 c iphone objective-c

我在我的实现文件中声明了一个枚举,如下所示,并在我的接口中将该类型的变量声明为PlayerState thePlayerState; 并在我的方法中使用了变量.但是我收到错误声明它是未声明的.如何在我的方法中正确声明和使用PlayerState类型的变量?:

在.m文件中

@implementation View1Controller

    typedef enum playerStateTypes
        {
            PLAYER_OFF,
            PLAYER_PLAYING,
            PLAYER_PAUSED
        } PlayerState;
Run Code Online (Sandbox Code Playgroud)

在.h文件中:

@interface View1Controller : UIViewController {

    PlayerState thePlayerState;
Run Code Online (Sandbox Code Playgroud)

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

-(void)doSomethin{

thePlayerState = PLAYER_OFF;

}
Run Code Online (Sandbox Code Playgroud)

reb*_*ach 205

Apple提供了一个宏来帮助提供更好的代码兼容性,包括Swift.使用宏看起来像这样.

typedef NS_ENUM(NSInteger, PlayerStateType) {
  PlayerStateOff,
  PlayerStatePlaying,
  PlayerStatePaused
};
Run Code Online (Sandbox Code Playgroud)

记录在这里

  • 第二个解决方案更好(使用`NS_ENUM`),因为它更现代,如果你想让你的枚举在Swift代码中可用,现在需要在Objective-C中. (3认同)

Dav*_*ong 109

typedef需要在头文件(或其他文件中#import插入标题),因为否则编译器将不知道使PlayerStateivar的大小.除此之外,它对我来说还不错.

  • +1.您在C中可以做的任何事情都适用于Objective-C. (16认同)
  • 应该建议使用NS_ENUM宏 - 因为这是最佳做法 (7认同)

Ben*_*ynn 28

在.h:

typedef enum {
    PlayerStateOff,
    PlayerStatePlaying,
    PlayerStatePaused
} PlayerState;
Run Code Online (Sandbox Code Playgroud)


sea*_*ard 19

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

typedef NS_ENUM(NSUInteger, PlayerState) {
        PLAYER_OFF,
        PLAYER_PLAYING,
        PLAYER_PAUSED
    };
Run Code Online (Sandbox Code Playgroud)

  • ...更重要的是,如果你希望你的枚举在Swift代码中可用,你必须在Objective-C中使用`NS_ENUM`声明枚举. (2认同)

San*_*gam 16

这就是Apple为NSString这样的类做的事情:

在头文件中:

enum {
    PlayerStateOff,
    PlayerStatePlaying,
    PlayerStatePaused
};

typedef NSInteger PlayerState;
Run Code Online (Sandbox Code Playgroud)

请参阅http://developer.apple.com/上的编码指南

  • 链接到developer.apple.com并没有多大帮助.你想引用其他一些地方吗? (23认同)
  • 这实际上并没有帮助OP.虽然技术上是正确的,但它没有告诉他们如何创建可重用的枚举 (3认同)
  • 这已经过时了,请参阅此页面https://developer.apple.com/library/ios/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObjective-C/AdoptingModernObjective-C.html (3认同)

Joh*_*nes 8

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

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

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
Run Code Online (Sandbox Code Playgroud)

.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
Run Code Online (Sandbox Code Playgroud)