oc枚举

本文介绍了在Objective-C中如何使用位操作来定义枚举类型,包括两种主要方式:单个使用(NS_ENUM)和组合使用(NS_OPTIONS)。通过实例展示了如何通过左移运算符实现枚举值的不同组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两种方案:

1:枚举NS_ENUM大部分单个使用:

typedef NS_ENUM(NSInteger, Test) {

    TestA = 1, //1 1 1

    TestB = 1 << 1, //2 2 10 转换成 10进制 2

    TestC = 1 << 2, //4 3 100 转换成 10进制 4

    TestD = 1 << 3, //8 4 1000 转换成 10进制 8

    TestE = 1 << 4 //16 5 10000 转换成 10进制 16

};

2:枚举NS_OPTIONS 可以多个拼接使用,通过位移实现:

typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};

使用:

UIViewAutoresizing option = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;

 NSLog(@"%ld",option);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值