如何定义和在Objective-C使用ENUM? [英] How do I define and use an ENUM in Objective-C?
问题描述
我宣布我实现文件的枚举,如下图所示,并宣称该类型在我的界面PlayerState thePlayerState的变量;和所用的变量在我的方法。但我得到的错误,指出它是未申报。我该如何正确申报,并在我的方法使用类型PlayerState的变量:
在.m文件
@implementation View1Controller 的typedef枚举playerStateTypes
{
PLAYER_OFF,
PLAYER_PLAYING,
PLAYER_PAUSED
} PlayerState;
在.h文件中:
@interface View1Controller:{的UIViewController PlayerState thePlayerState;
在.m文件的一些方法:
- (无效){doSomethinthePlayerState = PLAYER_OFF;}
您的typedef
需要在头文件(或其他文件中的#进口
编入你的头),否则编译器将不知道做什么尺寸 PlayerState
伊娃。除此之外,它看起来不错我。
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?:
In the .m file
@implementation View1Controller
typedef enum playerStateTypes
{
PLAYER_OFF,
PLAYER_PLAYING,
PLAYER_PAUSED
} PlayerState;
in the .h file:
@interface View1Controller : UIViewController {
PlayerState thePlayerState;
in some method in .m file:
-(void)doSomethin{
thePlayerState = PLAYER_OFF;
}
Your typedef
needs to be in the header file (or some other file that's #import
ed 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.
这篇关于如何定义和在Objective-C使用ENUM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!