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

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

问题描述

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

var state:MyEnum!

func myMethod()
{
如果状态! == MyEnum.FirstCase {
//做某事
}
}
}

如果二进制运算符'=='不能应用于两个'MyClass.MyEnum'
操作数


如果相反,我使用开关语句,没有问题:

 开关状态! {
//另外,为什么我需要```如果状态已经是
//隐式展开的可选项?是否因为可选项
//是内部枚举,编译器会感到困惑?

case .FirstCase:
//做某事...

默认值:
//(do nothing)
break
}

但是,开关感觉太冗长了:我只想要 .FirstCase 做一些,否则没有。一个如果语句更有意义。



枚举和 ==



编辑:这是非常奇怪的。在定位开关版本后,转移到我的代码的其他(完全不相关)部分,然后回来, if -statement版本(针对固定枚举大小写的comnparing force-unwrapped属性)正在编译而没有错误。
我只能得出结论,它与解析器中被清理的一些损坏的缓存有关。



编辑2 其他枚举情况(不是我在比较的对象) - 在这种情况下,.SecondCase )。我仍然不明白为什么特别会触发这个编译器错误(不能使用二进制运算符==...),或者这意味着什么。

解决方案

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



但是您可以使用模式匹配甚至在如果语句(自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)



在switch语句中,状态不会自动解开,
即使它是一个隐式解包的可选(否则你可以
不符合 nil )。但是在这里可以使用相同的模式:

  switch state {
case .FirstCase ?:
/ /做某事...
默认值:
break
}


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

    var state:MyEnum!

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

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

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

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
}

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 == ?

EDIT: 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 (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.

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 {

        }
    }
}

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

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
}

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

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