[英]How do I define and use an ENUM in Objective-C?
I declared an enum in my implementation file as shown below, and declared a variable of that type in my interface as PlayerState thePlayerState; 我在实现文件中声明了一个枚举,如下所示,并在我的接口中将该类型的变量声明为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类型的变量?:
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;
}
Apple provides a macro to help provide better code compatibility, including Swift. Apple提供了一个宏来帮助提供更好的代码兼容性,包括Swift。 Using the macro looks like this.
使用宏看起来像这样。
typedef NS_ENUM(NSInteger, PlayerStateType) {
PlayerStateOff,
PlayerStatePlaying,
PlayerStatePaused
};
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. 您的
typedef
必须位于头文件(或其他#import
到头文件中)中,因为否则编译器将不知道将PlayerState
ivar设置为什么大小。 Other than that, it looks ok to me. 除此之外,我觉得还可以。
In the .h: 在.h中:
typedef enum {
PlayerStateOff,
PlayerStatePlaying,
PlayerStatePaused
} PlayerState;
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
};
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/上的编码准则
I recommend using NS_OPTIONS or NS_ENUM. 我建议使用NS_OPTIONS或NS_ENUM。 You can read more about it here: http://nshipster.com/ns_enum-ns_options/
您可以在此处了解更多信息: 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. h。 file:
文件:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.