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
  • 314,917
  • 42
  • 532
  • 579
Emre Önder
  • 2,408
  • 2
  • 23
  • 73

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
  • 17,258
  • 4
  • 42
  • 73