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.

3 Answers 3

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"]
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.