在iOS中定義枚舉可以幫我們減輕不少工作
枚舉定義有兩種一種是數(shù)值 一種是按照位移
enum JQConnectState : NSInteger{
JQConnectStateDefault = 0,
JQConnectStateStart ,
JQConnectStatePause ,
JQConnectStateStop ,
};
typedef enum JQConnectState JQConnectState;
enum JQCarSupportOrientation : NSInteger{
JQCarSupportOrientationNone = 0 ,
JQCarSupportOrientationTop = 1 << 1 ,
JQCarSupportOrientationLeft = 1 << 2 ,
JQCarSupportOrientationRight = 1 << 3 ,
JQCarSupportOrientationButtom = 1 << 4 ,
};
typedef enum JQCarSupportOrientation JQCarSupportOrientation;
位移的用處在于可以組合使用,比如我們要判斷是否支持兩個(gè)方向的移動(dòng)。
return move| JQCarSupportOrientationLeft|JQCarSupportOrientationButtom;
iOS提供了兩個(gè)宏簡化枚舉的定義
typedef NS_ENUM(NSUInteger, JQConnectState) {
JQConnectStateDefault = 0,
JQConnectStateStart,
JQConnectStatePause,
JQConnectStateStop,
};
typedef NS_OPTIONS(NSUInteger, JQCarSupportOrientation) {
JQCarSupportOrientationNone = 0 ,
JQCarSupportOrientationTop = 1 << 1 ,
JQCarSupportOrientationLeft = 1 << 2 ,
JQCarSupportOrientationRight = 1 << 3 ,
JQCarSupportOrientationButtom = 1 << 4 ,
};
NS_ENUM用作通用 NS_OPTIONS用作位移枚舉
在定義枚舉的時(shí)候注意业踢,最好指定枚舉底層的數(shù)據(jù)
enum JQConnectState : NSIntege(數(shù)據(jù)類型)
NS_ENUM(NSUInteger(數(shù)據(jù)類型), JQConnectState)
另外在Effective-ObjC中也指出在switch分支中使用枚舉,盡量不要使用default陵像,應(yīng)該把枚舉的每個(gè)分支都包括