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

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

在.m文件中

@implementation View1Controller

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

在 .h 文件中:

@interface View1Controller : UIViewController {

    PlayerState thePlayerState;

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

-(void)doSomethin{

thePlayerState = PLAYER_OFF;

}

最佳答案

Apple 提供了一个宏来帮助提供更好的代码兼容性,包括 Swift。使用宏如下所示。

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

Documented here

https://stackoverflow.com/questions/2212080/

相关文章:

objective-c - Objective-C 中的 NULL 与 nil

objective-c - 项目构建中的 CocoaPods 错误

ios - 具有 UITapGestureRecognizer 的 View 中的 UIButton

ios - 在没有 UITableViewController 的情况下拉取刷新 UITableVi

ios - 在 Objective-C 中检查空字符串的正确方法是什么?

ios - 动态改变UILabel的字体大小

ios - 等到两个异步 block 被执行后再开始另一个 block

ios - 尝试将非属性列表对象设置为 NSUserDefaults

ios - iOS 7+ 中的 Base64 解码

ios - 从 UIView 到 UIViewController?