1

I'm trying to implement enum of array of strings like this

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)

But I'm getting this error:

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

Any of you knows how can fix this errors to make the enum work?

I'll really appreciate your help.

user2924482
  • 8,380
  • 23
  • 89
  • 173

3 Answers3

3

Remove [String] from the enum declaration.

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"]
        }
    }
}
McKay M
  • 448
  • 2
  • 8
0

You can set the raw value of your enum to String like this

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

Then access it like this

print(EstimateItemStatus.Pending.rawValue)
MNG MAN
  • 69
  • 1
  • 4
0

We can't tell what you actually need, because you're not using the Int or the Array. It's possible you want 2-String tuple raw values:

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]