将枚举类型的值与关联值进行比较时出现编译器错误? [英] Compiler error when comparing values of enum type with associated values?

查看:17
本文介绍了将枚举类型的值与关联值进行比较时出现编译器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class MyClass 
{
    enum MyEnum {
        case FirstCase
        case SecondCase(Int)
        case ThirdCase
    }

    var state:MyEnum!

    func myMethod ()
    {
        if state! == MyEnum.FirstCase {
            // Do something
        }
    }
}

我收到指向 if 语句的编译器错误::

I get the compiler error pointing at the if statement::

二元运算符=="不能应用于两个MyClass.MyEnum"操作数

Binary operator '==' cannot be applied to two 'MyClass.MyEnum' operands

如果相反,我使用switch 语句,则没有问题:

If instead, I use a switch statement, there is no problem:

switch state! {
    // Also, why do I need `!` if state is already an 
    // implicitly unwrapped optional? Is it because optionals also 
    // are internally enums, and the compiler gets confused?

case .FirstCase:
    // do something...

default:
    // (do nothing)
    break
}

然而,switch 语句感觉太冗长了:我只是想为 .FirstCase 做点什么,别的什么都不做.if 语句更有意义.

However, the switch statement feels too verbose: I just want to do something for .FirstCase, and nothing otherwise. An if statement makes more sense.

枚举和 == 是怎么回事?

What's going on with enums and == ?

这太奇怪了.在确定 switch 版本并转到我的代码的其他(完全不相关)部分后,返回到 if 语句版本(将 force-unwrapped 属性与固定枚举案例)正在编译,没有错误.我只能得出结论,它与解析器中的某些损坏的缓存有关,并且在此过程中被清除了.

This is ultra-weird. After settling for the switch version and moving on to other (totally unrelated) parts of my code, and coming back, the if-statement version (comnparing force-unwrapped property against fixed enum case) is compiling with no errors. I can only conclude that it has something to do with some corrupted cache in the parser that got cleared along the way.

EDIT 2(感谢 @LeoDabus 和 @MartinR):当我为 other 枚举案例(不是我的我比较 - 在​​这种情况下,.SecondCase).我仍然不明白为什么会特别触发这个编译器错误(不能使用二元运算符 '=='..."),或者这意味着什么.

EDIT 2 (Thanks @LeoDabus and @MartinR): It seems that the error appears when I set an associated value to the other enum case (not the one I am comparing against - in this case, .SecondCase). I still don't understand why that triggers this compiler error in particular ("Can't use binary operator '=='..."), or what that means.

推荐答案

正如你在评论中所说的,你的枚举类型实际上已经关联了值.在这种情况下,枚举类型没有默认的 == 运算符.

As you said in a comment, your enumeration type actually has associated values. In that case there is no default == operator for the enum type.

但是您甚至可以在 if 语句中使用模式匹配(从 Swift 2 开始):

But you can use pattern matching even in an if statement (since Swift 2):

class MyClass {
    enum MyEnum {
        case FirstCase
        case SecondCase
        case ThirdCase(Int)
    }

    var state:MyEnum!

    func myMethod () {
        if case .FirstCase? = state {

        }
    }
}

这里的 .FirstCase?.Some(MyEnum.FirstCase) 的快捷方式.

Here .FirstCase? is a shortcut for .Some(MyEnum.FirstCase).

在您的 switch 语句中,state 不会自动解包,即使它是一个隐式解包的可选(否则你可以与 nil 不匹配).但是这里可以使用相同的模式:

In your switch-statement, state is not automatically unwrapped, even if it is an implicitly unwrapped optional (otherwise you could not match against nil). But the same pattern can be used here:

switch state {
case .FirstCase?:
    // do something...
default:
    break
}

<小时>

更新:Swift 4.1 (Xcode 9.3) 开始,编译器可以为具有关联值的枚举合成 Equatable/Hashable 的一致性(如果它们的所有类型都是 Equatable/Hashable).声明一致性就足够了:


Update: As of Swift 4.1 (Xcode 9.3) the compiler can synthesize conformance to Equatable/Hashable for enums with associated values (if all their types are Equatable/Hashable). It suffices to declare the conformance:

class MyClass {
    enum MyEnum: Equatable {
        case firstCase
        case secondCase
        case thirdCase(Int)
    }

    var state:MyEnum!

    func myMethod () {
        if state  == .firstCase {
            // ...
        }
    }
}

这篇关于将枚举类型的值与关联值进行比较时出现编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆