1

我正在尝试实现这样的字符串数组枚举

import UIKit

enum EstimateItemStatus: Int, [String] {
    case Pending
    case OnHold
    case Done

    var description: [String] {
        switch self {
        case .Pending:
            return ["one", "Two"]
        case .OnHold:
            return ["one", "Two"]
        case .Done:
            return ["one", "Two"]
        }
    }
}

print(EstimateItemStatus.Pending.description)

但我收到了这个错误:

error: processArray.playground:3:31: error: multiple enum raw types 'Int' and '[String]'
enum EstimateItemStatus: Int, [String] {
                         ~~~  ^

你们中的任何人都知道如何修复此错误以使枚举工作?

我会非常感谢你的帮助。

4

3 回答 3

3

[String]从枚举声明中删除。

enum EstimateItemStatus: Int {
    case Pending
    case OnHold
    case Done

    var description: [String] {
        switch self {
        case .Pending:
            return ["one", "Two"]
        case .OnHold:
            return ["one", "Two"]
        case .Done:
            return ["one", "Two"]
        }
    }
}
于 2020-05-02T00:05:46.260 回答
0

我们无法说出您实际需要什么,因为您没有使用IntArray. 您可能需要2-String元组原始值:

enum EstimateItemStatus: CaseIterable {
  case pending
  case onHold
  case done
}

extension EstimateItemStatus: RawRepresentable {
  init?( rawValue: (String, String) ) {
    guard let `case` = ( Self.allCases.first { $0.rawValue == rawValue } )
    else { return nil }

    self = `case`
  }

  var rawValue: (String, String) {
    switch self {
    case .pending:
      return ("pending", "")
    case .onHold:
      return ("onHold", "")
    case .done:
      return ("done", "✅")
    }
  }
}
EstimateItemStatus( rawValue: ("onHold", "") )?.rawValue // ("onHold", "")
EstimateItemStatus( rawValue: ("Bootsy Collins", "") ) // nil
[("done", "✅"), ("pending", "")].map(EstimateItemStatus.init) // [done, pending]
于 2020-05-02T03:31:37.243 回答
0

您可以将枚举的原始值String设置为这样

enum EstimateItemStatus: String {
    case Pending: "Pending"
    case OnHold: "OnHold"
    case Done: "Done"
}

然后像这样访问它

print(EstimateItemStatus.Pending.rawValue)
于 2020-05-02T02:16:14.503 回答