iOS 位移枚举NS_OPTIONS(如何实现多个枚举值的同时传入判断)

一、场景

当我们使用枚举这个东西时,有时需要多个枚举值任一一个满足时就ture,但是常用的枚举NS_ENUM定义好的枚举只能挨个判断,写一坨就既不美观也不好阅读,如下:

c 复制代码
typedef NS_ENUM (NSInteger, RPTestType){
    RPTestTypeUnknown = 0, // unknow
    RPTestTypeValue0,      // 值0
    RPTestTypeValue1,      // 值1
    RPTestTypeValue2,      // 值2
    RPTestTypeValue3,      // 值3
    RPTestTypeValue4,      // 值4
    RPTestTypeValue5,      // 值5
};

RPTestType testTeype = RPTestTypeUnknown;
if (testTeype == RPTestTypeValue0 ||
    testTeype == RPTestTypeValue1 || 
    testTeype == RPTestTypeValue2 || 
    testTeype == RPTestTypeValue3 || 
    testTeype == RPTestTypeValue4 || 
    testTeype == RPTestTypeValue5) {
    NSLog(@"ture");
}

二、利用NS_OPTIONS优化

1、先了解一个关于位运算符的知识点:位运算符浅析

这里用到(右移 << )和 (按位与 & )和(按位或 | )这仨,先明确是怎么个事;

2、先看下NS_OPTIONS如何来定义枚举
c 复制代码
typedef NS_OPTIONS (NSInteger, RPTestType){
    RPTestTypeUnknown = 1 << 0, // unknow
    RPTestTypeValue0  = 1 << 1, // 值0
    RPTestTypeValue1  = 1 << 2, // 值1
    RPTestTypeValue2  = 1 << 3, // 值2
    RPTestTypeValue3  = 1 << 4, // 值3
    RPTestTypeValue4  = 1 << 5, // 值4
    RPTestTypeValue5  = 1 << 6, // 值5
};

这里使用右移定义枚举值,结合位运算可以这么看:

1 << 0 就是 0001,即2的0次方;

1 << 1 就是 0010,即2的1次方;

1 << 2 就是 0100,即2的2次方;

以此类推~

3、再看这么用的好处

当有多个枚举值都符合时做判断时候,可以直接(或 | )起来,然后(与 & )一下子,就能清晰明了的做判断;

c 复制代码
RPTestType testType = RPTestTypeUnknown;
RPTestType judgeType = RPTestTypeValue0 | RPTestTypeValue1 | RPTestTypeValue2 | RPTestTypeValue3 | RPTestTypeValue4 | RPTestTypeValue5;
if (testType & judgeType) {
    NSLog(@"ture");
}

当然最好使的用处是枚举值传参,如SDImage

c 复制代码
[self.testImgView sd_setImageWithURL:[NSURL URLWithString:@"xx"]
                    placeholderImage:nil
                             options:SDWebImageLowPriority | 
                                     SDWebImageProgressiveLoad |
                                     SDWebImageRefreshCached |
                                     SDWebImageContinueInBackground];

options这里可以直接传入多个枚举值,可用于方法内部与或运算进行判断,方便传值;类似的还有UIControlState;

c 复制代码
typedef NS_OPTIONS(NSUInteger, UIControlState) {
    UIControlStateNormal       = 0,
    UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
    UIControlStateDisabled     = 1 << 1,
    UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
    UIControlStateFocused API_AVAILABLE(ios(9.0)) = 1 << 3, // Applicable only when the screen supports focus
    UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
    UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
};
c 复制代码
[testBtn setTitle:@"xx"
         forState:UIControlStateNormal | 
                  UIControlStateHighlighted | 
                  UIControlStateSelected];
相关推荐
OKXLIN5 小时前
IOS UITextField 无法隐藏键盘问题
ios·objective-c
AL.千灯学长12 小时前
DeepSeek接入Siri(已升级支持苹果手表)完整版硅基流动DeepSeek-R1部署
人工智能·gpt·ios·ai·苹果vision pro
openinstall全渠道统计1 天前
免填邀请码工具:赋能六大核心场景,重构App增长新模型
android·ios·harmonyos
早起的年轻人2 天前
Flutter CupertinoNavigationBar iOS 风格导航栏的组件
flutter·ios
貂蝉空大2 天前
uni-app开发安卓和ios app 真机调试
android·ios·uni-app
胖虎12 天前
iOS 中的圆角与平滑圆角:从新特性到老项目适配
ios·圆角·平滑圆角·cornercurve
志飞2 天前
ios UICollectionView使用自定义UICollectionViewCell
ios·collectionview·自定义cell
Neo Evolution2 天前
Flutter与移动开发的未来:谷歌的技术愿景与实现路径
android·人工智能·学习·ios·前端框架·webview·着色器
没头脑的ht3 天前
ios App的启动过程和启动优化
ios
敲代码的鱼哇3 天前
设备唯一ID获取,支持安卓/iOS/鸿蒙Next(uni-device-id)UTS插件
android·ios·uniapp·harmonyos