0

I have an enum which has a String raw value. I want one of the cases get a string as input and return a string which that input. How can I achieve this?

public enum PredictTypes: String {
    case favtasks = "isFav == YES"
    case importtanttasks = "isImportant == YES"
    case alltasks = ""
    case customList(listName: String) = "listName == \(listName)"
}

As I searched I found some posts but can't understand for my case:

Can associated values and raw values coexist in Swift enumeration?

rmaddy
  • 307,833
  • 40
  • 508
  • 550
Emre Önder
  • 2,187
  • 2
  • 16
  • 58

1 Answers1

0

I would rewrite your enum like this:

public enum PredictTypes {
    case favtasks
    case importanttasks
    case alltasks
    case customList(listName: String)

    var rawValue: String {
        switch self {
        case .favtasks: return "isFav == YES"
        case .importanttasks: return "isImportant == YES"
        case .alltasks: return ""
        case .customList(let listName): return "listName == \(listName)"
        }
    }
}
Gereon
  • 16,040
  • 4
  • 38
  • 69