1.
enum direction{
east,
south,
west,
north
};
使用枚举类型:
enum direction dir;
dir = north;
定义类型时可以自由给各个元素赋值,赋值可以重复:
enum direction{
east,
south,
west = 10,
north
};
未赋值的元素值为其前一元素值+1,第一个元素值默认为0.
支持位运算的定义方式:
enum direction{
east = 0,
south = 1,
west = 1 << 1,
north = 1 << 2
};
示例中 east,south,west,north 分别对应二进制中的 0,1,10,100;即十进制中的 0,1,2,4.
2. 指定存储类型(integer):
enum direction : unsigned short int{
east,
south,
west,
north
};
3. 使用typedef定义别名
typedef enum{
east,
south,
west = 10,
north
} direction;
使用类型:
direction dir;
dir = north;
4. 使用 NS_ENUM 宏
typedef NS_ENUM(NSInteger, direction){
east,
south,
west = 10,
north
};
使用方式同方法3;