美文网首页
枚举值的添加

枚举值的添加

作者: 乐在琦中Helena | 来源:发表于2018-06-28 11:41 被阅读9次

之前遇到过这样一个问题,在一个先前设计好的枚举类型中添加或删除一个枚举值(删除是不可能的,这辈子都不可能的,最多只能是弃用),或组合一些枚举值,要如何做才能看起来不那么尬。

感到尬的原因可能只是当时设计的时候考虑的不够全面,也或许是这个枚举类型已经无法满足你的需求了,这时候就要换个思路,可以像苹果这样:

这是iOS6之前使用的:

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} __TVOS_PROHIBITED;

这是在iOS6之后添加的:

typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
} __TVOS_PROHIBITED;

先是对先前存在的枚举值进行了一个等价的平移,之后对每个值都进行了按位左移,这个的目的在于为新增的枚举值添加便利,可以通过按位或来实现。

真是一举两得。

相关文章

  • go 枚举类型

    这里需要用到enum库 定义一个枚举类型 操作枚举enum 查看枚举值 修改自定义枚举值 添加和移除枚举值

  • 枚举值的添加

    之前遇到过这样一个问题,在一个先前设计好的枚举类型中添加或删除一个枚举值(删除是不可能的,这辈子都不可能的,最多只...

  • Swift与OC的语法简单对比(常用语法二)

    20- 枚举,枚举原始值,枚举相关值,switch提取枚举关联值 Swift枚举: Swift中的枚举比OC中的枚...

  • 枚举类

    1.枚举类型的定义: 枚举类型定义的一般形式为 enum 枚举名{//枚举值表枚举值1;枚举值2;...} 在枚举...

  • Swift 5 枚举

    枚举 关联值: 枚举的成员值和其他类型的值关联储存,存储在枚举变量中 原始值: 枚举成员使用相同的默认值预先对应,...

  • Swift-枚举名、枚举值的相互转化

    通过枚举名获取到枚举值 或者 通过枚举值获取到枚举名称 .End

  • Swift 2 学习笔记 10.枚举

    课程来自慕课网liuyubobobo老师 枚举 枚举基础 枚举之原始值 枚举之关联值 枚举递归

  • swift基础——枚举

    枚举的基本用法 枚举的名称建议大写开头,成员名小写开头 枚举定义 枚举值使用 关联值 有时会将枚举的成员值跟其他类...

  • 枚举

    枚举 本节内容包括: 枚举语法 匹配枚举值与switch语句 相关值 原始值 枚举语法 注意:不像 C 和 Obj...

  • 枚举 函数 结构体 类

    import Foundation //枚举 /* enum 枚举名:值类型 { case 枚举情况 = 初始值 ...

网友评论

      本文标题:枚举值的添加

      本文链接:https://www.haomeiwen.com/subject/uelgyftx.html