class MyClass
{
enum MyEnum {
case FirstCase
case SecondCase
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 == ?
Aucun commentaire:
Enregistrer un commentaire