Objective-C定义枚举类型的几种方式

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
        };


使用方式同方法1.


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;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值