提问者:小点点

如何比较枚举忽略关联值?[重复]


如何在忽略关联值的情况下检查枚举的大小写?

下面是我使用的,但它给出了一个错误…

enum Example {
        case one(value: String)
        case two(otherValue: Int)
}

var test = Example.one(value: "A String")

if test == Example.one {   // Gives Error 
// Do Something
}

重复问题过于复杂。


共1个答案

匿名用户

改用下面的if case语句:

enum Example {
    case one(value: String)
    case two(otherValue: Int)
}

var test = Example.one(value: "A String")

if case Example.one(value: _) = test {   // Works
    // Do Something
}