簡單枚舉類型
enum EOCConnectionState {
EOCConnectionStateDisconnected,
EOCConnectionStateConnecting,
EOCConnectionStateConnected,
}
typedef enum EOCConnectionState EOCConnectionState;
這樣就可以簡寫的EOCConnectionState來代替完整的enum EOCConnectionState。
指定底層數(shù)據(jù)類型
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,
...
}
用枚舉定義定義狀態(tài)機,最好不要有default分支。
switch(_currentState) {
case EOCConnectionStateDisconnected:
break;
case EOCConnectionStateConnecting:
break;
case EOCConnectionStateConnected:
break;
}