【问题标题】:enum rawValue constructor ignoring case枚举 rawValue 构造函数忽略大小写
【发布时间】:2016-04-27 13:13:21
【问题描述】:
enum DocumentType : String
{
    case
    Any = "Any",
    DL = "DL",
    Passport = "Passport",
    Invalid
}

我正在像这样使用 rawValue 构造函数

if let d = DocumentType(rawValue: type) {

解析来自服务器的任何内容。

现在,假设服务器端的暗灯泡将 DL 更改为 Dl 服务器端 -> 解析器中断默认为无效。

是否有针对 Windows 开发人员对解析器进行校对的规定 没有手动写很长的if else daisy? 与白痴改变 json 中键的大小写相同的问题。 需要某种方式以一种不太好的开发者抵抗方式读取 json。 谢谢。

【问题讨论】:

标签: swift2


【解决方案1】:

我认为您不能忽略大小写,但您可以创建一个初始化程序来防止您的特定问题,而无需使用 if/else 语句链:

enum DocumentType : String
{
    case
    Any = "Any",
    DL = "DL",
    Passport = "Passport",
    Invalid
    init?(raw:String) {
        let str = raw.characters.count > 2 ?  raw.capitalizedString : raw.uppercaseString
        if let d = DocumentType(rawValue: str) {
            self = d
        }
        else {
            return nil
        }
    }

}

if let d = DocumentType(raw: "ANY") {
    print("success!")
}

【讨论】:

    【解决方案2】:

    我们可以通过在初始化枚举时改变大小写来做到这一点。

     enum DocumentType : String {
        case any = "ANY"
        case dl = "DL"
        case passport = "PASSPORT"
        case invalid = "INVALID"
    
        //If you want to display status in UI define your display name
        var documentDisplayType: String {
         switch self  {
           case .any:
                return "Any"
           case .dl:
                return "Driving License"
           case .passport:
                return "Passport"
           case invalid:
                return "Not proper document"
        }
    }
    

    在初始化时,使用 uppercased() 函数将响应文档类型更改为大写。

     let type = "Dl"
     let docType = DocumentType(rawValue: type.uppercased())
     print(docType.documentDisplayType) // Prints DL
    

    【讨论】:

      【解决方案3】:

      适用于所有相关枚举的通用扩展。 Enum 必须遵守 StringCaseIterable 协议才能正常工作。

      import Foundation
      extension CaseIterable where Self:RawRepresentable, RawValue == String  {
          init?(rawValueIgnoreCase: RawValue) {
              if let caseFound = Self.allCases.first(where: { $0.rawValue.caseInsensitiveCompare(rawValueIgnoreCase) == .orderedSame }) {
                  self = caseFound
              } else {
                  self.init(rawValue: rawValueIgnoreCase)
              }
          }
      }
      
      enum FooBar: String, CaseIterable {
          case foo
          case bar = "BAR"
      }
      
      let foo = FooBar(rawValueIgnoreCase: "foo")
      let FOO = FooBar(rawValueIgnoreCase: "FOO")
      let FoO = FooBar(rawValueIgnoreCase: "FoO")
      let bar = FooBar(rawValueIgnoreCase: "bar")
      let Bar = FooBar(rawValueIgnoreCase: "Bar")
      let BAR = FooBar(rawValueIgnoreCase: "BAR")
      let oops = FooBar(rawValueIgnoreCase: "oops") // nil
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-20
        • 2011-10-30
        • 1970-01-01
        相关资源
        最近更新 更多