C语言 如何在 Objective-C 中定义和使用 ENUM?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2212080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I define and use an ENUM in Objective-C?
提问by RexOnRoids
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?:
我在我的实现文件中声明了一个枚举,如下所示,并在我的界面中声明了一个该类型的变量作为 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;
}
采纳答案by Dave DeLong
Your typedefneeds 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 PlayerStateivar. Other than that, it looks ok to me.
你typedef需要在头文件(或其他一些被#import编辑到你的头文件中的文件)中,否则编译器将不知道生成PlayerStateivar 的大小。除此之外,在我看来还可以。
回答by rebelzach
Apple provides a macro to help provide better code compatibility, including Swift. Using the macro looks like this.
Apple 提供了一个宏来帮助提供更好的代码兼容性,包括 Swift。使用宏看起来像这样。
typedef NS_ENUM(NSInteger, PlayerStateType) {
PlayerStateOff,
PlayerStatePlaying,
PlayerStatePaused
};
回答by Ben Flynn
In the .h:
在.h中:
typedef enum {
PlayerStateOff,
PlayerStatePlaying,
PlayerStatePaused
} PlayerState;
回答by sean woodward
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
};
回答by Santhos
This is how Apple does it for classes like NSString:
这就是 Apple 对 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/上的编码指南
回答by Johannes
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

