代码之家  ›  专栏  ›  技术社区  ›  Adrian rohit chauhan

在Swift中将结构重构为枚举

  •  2
  • Adrian rohit chauhan  · 技术社区  · 7 年前

    enum :

    typealias PolicyType = (filename: String, text: String)
    
    struct Policy {
      static let first = PolicyType(filename: "firstFile.txt", text: "text in first file")
      static let second = PolicyType(filename: "secondFile.txt", text: "text in second file")
      static let third = PolicyType(filename: "thirdFile.txt", text: "text in third file")
    }
    
    let thirdPolicyText = Policy.third.text
    

    下面是我想到的:

    enum Policy: RawRepresentable {
      case one
      case two
      case three
    
      var rawValue: (filename: String, text: String) {
        switch self {
        case .one:
          return ("1", "policy 1 text")
        case .two:
          return ("2", "policy 2 text")
        case .three:
          return ("3", "policy 3 text")
        }
      }
    
      init?(rawValue: (filename: String, text: String)) {
        switch rawValue {
        case ("1", "policy 1 text"):
          self = .one
        case ("2", "policy 2 text"):
          self = .two
        case ("3", "policy 3 text"):
          self = .three
        default:
          return nil
        }
      }
    }
    

    在这一点上,我已经想出了如何用 struct 和一个 枚举 . 这个 如果有人回去更新它,似乎需要更多的维护,而且更容易出错。保罗赫加蒂说,不会崩溃的行是你不写的行和 枚举 路线看起来和感觉都很麻烦。

    去看电影有记忆优势吗 枚举 路线与a 结构

    完成后,我希望能够将策略作为参数传递,如下所示:

    func test(for policy: Policy) {
      print(policy.rawValue.filename)
      print(policy.rawValue.text)
    }
    
    test(for: Policy.first)
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Cristik    7 年前

    struct 关键字 enum

    enum Policy {
      static let first = PolicyType(filename: "firstFile.txt", text: "text in first file")
      static let second = PolicyType(filename: "secondFile.txt", text: "text in second file")
      static let third = PolicyType(filename: "thirdFile.txt", text: "text in third file")
    }
    

    没有案例的枚举不可能实例化,这样开发人员就只能使用定义的静态策略类型。

    这样的函数:

    func cantBeCalled(policy: Policy) { }
    

    Policy .

    struct Policy {
        static let first = Policy(filename: "firstFile.txt", text: "text in first file")
        static let second = Policy(filename: "secondFile.txt", text: "text in second file")
        static let third = Policy(filename: "thirdFile.txt", text: "text in third file")
    
        public let filename: String
        public let text: String
    
        private init(filename: String, text: String) {
            self.filename = filename
            self.text = text
        }
    }
    

    结构可以更好地映射问题域,因为您需要一个包含这两个属性的容器。当细节的数量增加时,元组不擅长伸缩,而且更难操作(例如,它们不能符合协议)。

    这种新的结构设计具有与上面枚举相同的优点:不能“手工”构造策略,只有一组预定义的策略可用。并且带来了更多的好处:只有一种类型,更好的容器语义,能够在服务器上使用协议 政策 类型,对属性没有不明确的访问(可以通过标签或索引访问已标记元组的成员)。

        2
  •  3
  •   Daniel    7 年前

    有一种可能性。有点长,但很快:

    enum Policy {
        case one, two, three
    
        var filename: String {
            switch self {
            case .one: return "Policy 1 name"
            case .two: return "Policy 2 name"
            case .three: return "Policy 3 name"
            }
        }
    
        var text: String {
            switch self {
            case .one: return "Policy 1 text"
            case .two: return "Policy 2 text"
            case .three: return "Policy 3 text"
            }
        }
    }