简单枚举类型
enum EOCConnectionState {
EOCConnectionStateDisconnected,
EOCConnectionStateConnecting,
EOCConnectionStateConnected,
}
typedef enum EOCConnectionState EOCConnectionState;
这样就可以简写的EOCConnectionState来代替完整的enum EOCConnectionState。
指定底层数据类型
enum EOCConnectionState : NSInteger {/* ... */}
或者向前声明:
enum EOCConnectionState : NSInteger;
彼此组合的枚举
各选项之间可以通过”按位或操作符”。
enum UIViewAutoresizing {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
...
}
Foundation框架辅助宏
这些宏具有向后兼容能力。
typedef NS_ENUM(NSUInteger, EOCConnectionState) {
EOConnectionStateDisconnected,
...
}
typedef NS_OPTIONS(NSUInteger, EOCPermittedDirection) {
EOCPermittedDirectionUp = 1 << 0,
...
}
用枚举定义定义状态机,最好不要有default分支。
switch(_currentState) {
case EOCConnectionStateDisconnected:
break;
case EOCConnectionStateConnecting:
break;
case EOCConnectionStateConnected:
break;
}