V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
banxi1988
V2EX  ›  iDev

[Swift] Enum 好用, Enum 可以更易用

  •  
  •   banxi1988 ·
    banxi1988 · 2016-12-24 14:33:18 +08:00 · 3205 次点击
    这是一个创建于 2696 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Swift 中的枚举可以说好用到让我惊讶. 但是我可以让它更易用.

    经典用例

    比如 用它来封装 应用 微信的 Tab 栏枚举,如下:

    enum AppTab{
        case wechat,contacts,discover,me
    }
    

    到这里已经比用整型常量好很多了. 好了,现在你想将 UITabBarItem 中的构造封装在里面.

    1. 首先我们为其添加一个 title 属性,如下:
    extension AppTab{
      var title:String{
        switch self {
        case .wechat: return "微信"
        case .contacts: return "通讯录"
        case .discover: return "发现"
        case .me: return "我"
        }
      }
    }
    
    

    然后.就可以直接使用 .title 就可以访问了. 避免了 使用字典保存映射的麻烦. 写法,修改也简单.

    1. 你想判断某一个 AppTab 值是不是 me. 你选择这样做 if tab == .wechat 嗯, Swift 中可以直接写 .wechat 这样的枚举值真方便. 但是你也可以这样. 添加一个 Bool 类型的 Computed Property.
    extension AppTab{
      var isWechat:Bool{ return self == .wechat }
    }
    

    然后你想为所有的枚举值都加上这样的 Computed Property. 你复制粘贴然后修改.

    1. 很多时候,你想遍历枚举值, 于是你添加了一个静态属性.保存所以的枚举值为一个数组. 如下:
    extension AppTab{
      static let allCases:[ AppTab] = [.wechat, .contacts, .discover, .me]
    }
    

    动起来

    so far, so good. 那我可以帮到你什么呢? 我可以帮你少写代码. 怎么帮?

    1. 只需要写少量的几行声明:

    如下.

    AppTab
    wechat:微信
    contacts: 通讯录
    discover:发现
    me:我
    

    选中, 右键, 选择 "Services|generate_enum" 然后如下代码就自动生成了:

    //AppTab
    //wechat:微信
    //contacts: 通讯录
    //discover:发现
    //me:我
    enum AppTab  {
         case wechat, contacts,discover,me
        var isWechat:Bool{ return self == .wechat }
        var isContacts:Bool{ return self == .contacts }
        var isDiscover:Bool{ return self == .discover }
        var isMe:Bool{  return self == .me }
        var title:String{
            switch self{
            case .wechat:return "微信"
            case .contacts:return " 通讯录"
            case .discover:return "发现"
            case .me:return "我"
            }
        }
        static let allCases:[AppTab] = [.wechat,.contacts,.discover,.me]
    }
    

    怎么样? 来试试吧! 当然还有其他选项可以使用. 如果你有其他需要麻烦告诉我. 当然有 PR 最好了.

    最后来张动图感受一下:

    Generate Enum

    代码在:

    OSC Git

    Github

    2 条回复    2016-12-24 21:06:41 +08:00
    pango
        1
    pango  
       2016-12-24 15:57:33 +08:00
    swift3 是地球上最好的语言 doge:)
    iyeatse
        2
    iyeatse  
       2016-12-24 21:06:41 +08:00
    @pango swift 3 没动态派发, swift 4 没原生异步, swift 5 才是地球最好语言
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2906 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 458ms · UTC 09:34 · PVG 17:34 · LAX 02:34 · JFK 05:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.