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)
Dav*_*ong 109
您typedef需要在头文件(或其他文件中#import插入标题),因为否则编译器将不知道使PlayerStateivar的大小.除此之外,它对我来说还不错.
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)
San*_*gam 16
这就是Apple为NSString这样的类做的事情:
在头文件中:
enum {
PlayerStateOff,
PlayerStatePlaying,
PlayerStatePaused
};
typedef NSInteger PlayerState;
Run Code Online (Sandbox Code Playgroud)
请参阅http://developer.apple.com/上的编码指南
我建议使用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)
| 归档时间: |
|
| 查看次数: |
165798 次 |
| 最近记录: |