如何实现Enum对个案进行论证

时间:2019-06-22 13:26:34

标签: swift enums

我有一个具有String原始值的枚举。我希望其中一种情况得到一个字符串作为输入并返回该输入的字符串。我该如何实现?

data = [183948, 218520, 243141, 224539, 205322, 203855, 233281, 244830, 281245,
 280579, 235384, 183596, 106072,  88773,  63297,  38769,  28343]

sizes = 3, 10

np.add.reduceat(data, np.cumsum([0, *sizes]))
# array([ 645609, 2198703,  219182])

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

Can associated values and raw values coexist in Swift enumeration?

1 个答案:

答案 0 :(得分:0)

我会这样重写您的枚举:

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