【问题标题】:How can achieve Enum to have a arguments on a case如何实现 Enum 对案例有论据
【发布时间】:2019-06-22 13:26:34
【问题描述】:

我有一个具有字符串原始值的枚举。我希望其中一种情况将字符串作为输入并返回该输入的字符串。我怎样才能做到这一点?

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

当我搜索时,我发现了一些帖子,但无法理解我的情况:

Can associated values and raw values coexist in Swift enumeration?

【问题讨论】:

    标签: swift enums


    【解决方案1】:

    我会像这样重写你的枚举:

    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)"
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多