Compiler error when comparing values of enum type with associated values?












2















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.










share|improve this question

























  • you forgot to initialise state and you are forcing unwrapping it. add guard let state = state else { return } instead

    – Leo Dabus
    Nov 6 '15 at 6:09













  • This is dummy code with type and variable names changed, and most methods omitted. In my real code, the var is initialized. Either case, that should be a runtime issue at most. In reality, I am testing the value in viewDidLoad(), and the compiler can't know if it is initialized or not.

    – Nicolas Miari
    Nov 6 '15 at 6:15













  • What Xcode version are you using? I don't get this error here

    – Leo Dabus
    Nov 6 '15 at 6:19








  • 1





    Is that your real code? As Leo said, it compiles without problem in Xcode 7 and 7.1. – Perhaps you have an enum with associated values?

    – Martin R
    Nov 6 '15 at 6:27








  • 1





    @NicolasMiari: Another option is to make your enum conform to Equatable, as seen here: stackoverflow.com/a/25726677/59541

    – Nate Cook
    Nov 6 '15 at 7:21
















2















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.










share|improve this question

























  • you forgot to initialise state and you are forcing unwrapping it. add guard let state = state else { return } instead

    – Leo Dabus
    Nov 6 '15 at 6:09













  • This is dummy code with type and variable names changed, and most methods omitted. In my real code, the var is initialized. Either case, that should be a runtime issue at most. In reality, I am testing the value in viewDidLoad(), and the compiler can't know if it is initialized or not.

    – Nicolas Miari
    Nov 6 '15 at 6:15













  • What Xcode version are you using? I don't get this error here

    – Leo Dabus
    Nov 6 '15 at 6:19








  • 1





    Is that your real code? As Leo said, it compiles without problem in Xcode 7 and 7.1. – Perhaps you have an enum with associated values?

    – Martin R
    Nov 6 '15 at 6:27








  • 1





    @NicolasMiari: Another option is to make your enum conform to Equatable, as seen here: stackoverflow.com/a/25726677/59541

    – Nate Cook
    Nov 6 '15 at 7:21














2












2








2








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.










share|improve this question
















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.







swift if-statement enums binary-operators






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 28 '16 at 11:35









Martin R

395k55863965




395k55863965










asked Nov 6 '15 at 5:12









Nicolas MiariNicolas Miari

10.2k551127




10.2k551127













  • you forgot to initialise state and you are forcing unwrapping it. add guard let state = state else { return } instead

    – Leo Dabus
    Nov 6 '15 at 6:09













  • This is dummy code with type and variable names changed, and most methods omitted. In my real code, the var is initialized. Either case, that should be a runtime issue at most. In reality, I am testing the value in viewDidLoad(), and the compiler can't know if it is initialized or not.

    – Nicolas Miari
    Nov 6 '15 at 6:15













  • What Xcode version are you using? I don't get this error here

    – Leo Dabus
    Nov 6 '15 at 6:19








  • 1





    Is that your real code? As Leo said, it compiles without problem in Xcode 7 and 7.1. – Perhaps you have an enum with associated values?

    – Martin R
    Nov 6 '15 at 6:27








  • 1





    @NicolasMiari: Another option is to make your enum conform to Equatable, as seen here: stackoverflow.com/a/25726677/59541

    – Nate Cook
    Nov 6 '15 at 7:21



















  • you forgot to initialise state and you are forcing unwrapping it. add guard let state = state else { return } instead

    – Leo Dabus
    Nov 6 '15 at 6:09













  • This is dummy code with type and variable names changed, and most methods omitted. In my real code, the var is initialized. Either case, that should be a runtime issue at most. In reality, I am testing the value in viewDidLoad(), and the compiler can't know if it is initialized or not.

    – Nicolas Miari
    Nov 6 '15 at 6:15













  • What Xcode version are you using? I don't get this error here

    – Leo Dabus
    Nov 6 '15 at 6:19








  • 1





    Is that your real code? As Leo said, it compiles without problem in Xcode 7 and 7.1. – Perhaps you have an enum with associated values?

    – Martin R
    Nov 6 '15 at 6:27








  • 1





    @NicolasMiari: Another option is to make your enum conform to Equatable, as seen here: stackoverflow.com/a/25726677/59541

    – Nate Cook
    Nov 6 '15 at 7:21

















you forgot to initialise state and you are forcing unwrapping it. add guard let state = state else { return } instead

– Leo Dabus
Nov 6 '15 at 6:09







you forgot to initialise state and you are forcing unwrapping it. add guard let state = state else { return } instead

– Leo Dabus
Nov 6 '15 at 6:09















This is dummy code with type and variable names changed, and most methods omitted. In my real code, the var is initialized. Either case, that should be a runtime issue at most. In reality, I am testing the value in viewDidLoad(), and the compiler can't know if it is initialized or not.

– Nicolas Miari
Nov 6 '15 at 6:15







This is dummy code with type and variable names changed, and most methods omitted. In my real code, the var is initialized. Either case, that should be a runtime issue at most. In reality, I am testing the value in viewDidLoad(), and the compiler can't know if it is initialized or not.

– Nicolas Miari
Nov 6 '15 at 6:15















What Xcode version are you using? I don't get this error here

– Leo Dabus
Nov 6 '15 at 6:19







What Xcode version are you using? I don't get this error here

– Leo Dabus
Nov 6 '15 at 6:19






1




1





Is that your real code? As Leo said, it compiles without problem in Xcode 7 and 7.1. – Perhaps you have an enum with associated values?

– Martin R
Nov 6 '15 at 6:27







Is that your real code? As Leo said, it compiles without problem in Xcode 7 and 7.1. – Perhaps you have an enum with associated values?

– Martin R
Nov 6 '15 at 6:27






1




1





@NicolasMiari: Another option is to make your enum conform to Equatable, as seen here: stackoverflow.com/a/25726677/59541

– Nate Cook
Nov 6 '15 at 7:21





@NicolasMiari: Another option is to make your enum conform to Equatable, as seen here: stackoverflow.com/a/25726677/59541

– Nate Cook
Nov 6 '15 at 7:21












2 Answers
2






active

oldest

votes


















7














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
}




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 {
// ...
}
}
}





share|improve this answer


























  • Thank you very much. I ended up not needing the associated value in my current code, so the if statement works as initially planned, but will remember this pattern for future reference.

    – Nicolas Miari
    Nov 6 '15 at 6:38











  • @Martin R: Optional(E.A) == E.A // gives me true in Swift 2

    – user3441734
    Nov 6 '15 at 6:44











  • @user3441734: Sorry, I don't get what you are asking.

    – Martin R
    Nov 6 '15 at 6:47











  • Any way to do this in one line? E.g. let isEqual: Bool = .FirstCase == state

    – BallpointBen
    Jul 25 '17 at 22:17






  • 1





    From Swift 4.1 due to SE-0185, Swift also supports synthesizing Equatable and Hashable for enums with associated values.

    – jedwidz
    Nov 22 '18 at 12:51



















1














class MyClass {
enum MyEnum {
case FirstCase
case SecondCase
case ThirdCase
}

var state: MyEnum!

func myMethod() {
guard
let state = state else { return }

if state == MyEnum.FirstCase {
// Do something
print(true)
} else {
print(false)
}
}
}


let myClass = MyClass()
myClass.state = .FirstCase
myClass.myMethod()
myClass.state = .SecondCase
myClass.myMethod()





share|improve this answer
























  • What happens if instead of first unwrapping with guard, you skip it and just try to compare state! == MyEnum.FirstCase?

    – Nicolas Miari
    Nov 6 '15 at 6:21











  • @NicolasMiari your code runs fine here as long as you initialise state property

    – Leo Dabus
    Nov 6 '15 at 6:23











  • I repeat: the compiler (static analyzer?) has no way to know if the property is initialized or not at the location where it is used (viewDidLoad), and I am force-unwrapping it (which I souldn't need to, since it is an implicitly unwrapped optional to begin with). And the compiler error I was getting had nothing to do with initialization, but with comparing two enums of the same type (?????)

    – Nicolas Miari
    Nov 6 '15 at 6:30











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33559936%2fcompiler-error-when-comparing-values-of-enum-type-with-associated-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









7














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
}




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 {
// ...
}
}
}





share|improve this answer


























  • Thank you very much. I ended up not needing the associated value in my current code, so the if statement works as initially planned, but will remember this pattern for future reference.

    – Nicolas Miari
    Nov 6 '15 at 6:38











  • @Martin R: Optional(E.A) == E.A // gives me true in Swift 2

    – user3441734
    Nov 6 '15 at 6:44











  • @user3441734: Sorry, I don't get what you are asking.

    – Martin R
    Nov 6 '15 at 6:47











  • Any way to do this in one line? E.g. let isEqual: Bool = .FirstCase == state

    – BallpointBen
    Jul 25 '17 at 22:17






  • 1





    From Swift 4.1 due to SE-0185, Swift also supports synthesizing Equatable and Hashable for enums with associated values.

    – jedwidz
    Nov 22 '18 at 12:51
















7














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
}




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 {
// ...
}
}
}





share|improve this answer


























  • Thank you very much. I ended up not needing the associated value in my current code, so the if statement works as initially planned, but will remember this pattern for future reference.

    – Nicolas Miari
    Nov 6 '15 at 6:38











  • @Martin R: Optional(E.A) == E.A // gives me true in Swift 2

    – user3441734
    Nov 6 '15 at 6:44











  • @user3441734: Sorry, I don't get what you are asking.

    – Martin R
    Nov 6 '15 at 6:47











  • Any way to do this in one line? E.g. let isEqual: Bool = .FirstCase == state

    – BallpointBen
    Jul 25 '17 at 22:17






  • 1





    From Swift 4.1 due to SE-0185, Swift also supports synthesizing Equatable and Hashable for enums with associated values.

    – jedwidz
    Nov 22 '18 at 12:51














7












7








7







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
}




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 {
// ...
}
}
}





share|improve this answer















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
}




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 {
// ...
}
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 20:48

























answered Nov 6 '15 at 6:36









Martin RMartin R

395k55863965




395k55863965













  • Thank you very much. I ended up not needing the associated value in my current code, so the if statement works as initially planned, but will remember this pattern for future reference.

    – Nicolas Miari
    Nov 6 '15 at 6:38











  • @Martin R: Optional(E.A) == E.A // gives me true in Swift 2

    – user3441734
    Nov 6 '15 at 6:44











  • @user3441734: Sorry, I don't get what you are asking.

    – Martin R
    Nov 6 '15 at 6:47











  • Any way to do this in one line? E.g. let isEqual: Bool = .FirstCase == state

    – BallpointBen
    Jul 25 '17 at 22:17






  • 1





    From Swift 4.1 due to SE-0185, Swift also supports synthesizing Equatable and Hashable for enums with associated values.

    – jedwidz
    Nov 22 '18 at 12:51



















  • Thank you very much. I ended up not needing the associated value in my current code, so the if statement works as initially planned, but will remember this pattern for future reference.

    – Nicolas Miari
    Nov 6 '15 at 6:38











  • @Martin R: Optional(E.A) == E.A // gives me true in Swift 2

    – user3441734
    Nov 6 '15 at 6:44











  • @user3441734: Sorry, I don't get what you are asking.

    – Martin R
    Nov 6 '15 at 6:47











  • Any way to do this in one line? E.g. let isEqual: Bool = .FirstCase == state

    – BallpointBen
    Jul 25 '17 at 22:17






  • 1





    From Swift 4.1 due to SE-0185, Swift also supports synthesizing Equatable and Hashable for enums with associated values.

    – jedwidz
    Nov 22 '18 at 12:51

















Thank you very much. I ended up not needing the associated value in my current code, so the if statement works as initially planned, but will remember this pattern for future reference.

– Nicolas Miari
Nov 6 '15 at 6:38





Thank you very much. I ended up not needing the associated value in my current code, so the if statement works as initially planned, but will remember this pattern for future reference.

– Nicolas Miari
Nov 6 '15 at 6:38













@Martin R: Optional(E.A) == E.A // gives me true in Swift 2

– user3441734
Nov 6 '15 at 6:44





@Martin R: Optional(E.A) == E.A // gives me true in Swift 2

– user3441734
Nov 6 '15 at 6:44













@user3441734: Sorry, I don't get what you are asking.

– Martin R
Nov 6 '15 at 6:47





@user3441734: Sorry, I don't get what you are asking.

– Martin R
Nov 6 '15 at 6:47













Any way to do this in one line? E.g. let isEqual: Bool = .FirstCase == state

– BallpointBen
Jul 25 '17 at 22:17





Any way to do this in one line? E.g. let isEqual: Bool = .FirstCase == state

– BallpointBen
Jul 25 '17 at 22:17




1




1





From Swift 4.1 due to SE-0185, Swift also supports synthesizing Equatable and Hashable for enums with associated values.

– jedwidz
Nov 22 '18 at 12:51





From Swift 4.1 due to SE-0185, Swift also supports synthesizing Equatable and Hashable for enums with associated values.

– jedwidz
Nov 22 '18 at 12:51













1














class MyClass {
enum MyEnum {
case FirstCase
case SecondCase
case ThirdCase
}

var state: MyEnum!

func myMethod() {
guard
let state = state else { return }

if state == MyEnum.FirstCase {
// Do something
print(true)
} else {
print(false)
}
}
}


let myClass = MyClass()
myClass.state = .FirstCase
myClass.myMethod()
myClass.state = .SecondCase
myClass.myMethod()





share|improve this answer
























  • What happens if instead of first unwrapping with guard, you skip it and just try to compare state! == MyEnum.FirstCase?

    – Nicolas Miari
    Nov 6 '15 at 6:21











  • @NicolasMiari your code runs fine here as long as you initialise state property

    – Leo Dabus
    Nov 6 '15 at 6:23











  • I repeat: the compiler (static analyzer?) has no way to know if the property is initialized or not at the location where it is used (viewDidLoad), and I am force-unwrapping it (which I souldn't need to, since it is an implicitly unwrapped optional to begin with). And the compiler error I was getting had nothing to do with initialization, but with comparing two enums of the same type (?????)

    – Nicolas Miari
    Nov 6 '15 at 6:30
















1














class MyClass {
enum MyEnum {
case FirstCase
case SecondCase
case ThirdCase
}

var state: MyEnum!

func myMethod() {
guard
let state = state else { return }

if state == MyEnum.FirstCase {
// Do something
print(true)
} else {
print(false)
}
}
}


let myClass = MyClass()
myClass.state = .FirstCase
myClass.myMethod()
myClass.state = .SecondCase
myClass.myMethod()





share|improve this answer
























  • What happens if instead of first unwrapping with guard, you skip it and just try to compare state! == MyEnum.FirstCase?

    – Nicolas Miari
    Nov 6 '15 at 6:21











  • @NicolasMiari your code runs fine here as long as you initialise state property

    – Leo Dabus
    Nov 6 '15 at 6:23











  • I repeat: the compiler (static analyzer?) has no way to know if the property is initialized or not at the location where it is used (viewDidLoad), and I am force-unwrapping it (which I souldn't need to, since it is an implicitly unwrapped optional to begin with). And the compiler error I was getting had nothing to do with initialization, but with comparing two enums of the same type (?????)

    – Nicolas Miari
    Nov 6 '15 at 6:30














1












1








1







class MyClass {
enum MyEnum {
case FirstCase
case SecondCase
case ThirdCase
}

var state: MyEnum!

func myMethod() {
guard
let state = state else { return }

if state == MyEnum.FirstCase {
// Do something
print(true)
} else {
print(false)
}
}
}


let myClass = MyClass()
myClass.state = .FirstCase
myClass.myMethod()
myClass.state = .SecondCase
myClass.myMethod()





share|improve this answer













class MyClass {
enum MyEnum {
case FirstCase
case SecondCase
case ThirdCase
}

var state: MyEnum!

func myMethod() {
guard
let state = state else { return }

if state == MyEnum.FirstCase {
// Do something
print(true)
} else {
print(false)
}
}
}


let myClass = MyClass()
myClass.state = .FirstCase
myClass.myMethod()
myClass.state = .SecondCase
myClass.myMethod()






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 6 '15 at 6:16









Leo DabusLeo Dabus

132k31270345




132k31270345













  • What happens if instead of first unwrapping with guard, you skip it and just try to compare state! == MyEnum.FirstCase?

    – Nicolas Miari
    Nov 6 '15 at 6:21











  • @NicolasMiari your code runs fine here as long as you initialise state property

    – Leo Dabus
    Nov 6 '15 at 6:23











  • I repeat: the compiler (static analyzer?) has no way to know if the property is initialized or not at the location where it is used (viewDidLoad), and I am force-unwrapping it (which I souldn't need to, since it is an implicitly unwrapped optional to begin with). And the compiler error I was getting had nothing to do with initialization, but with comparing two enums of the same type (?????)

    – Nicolas Miari
    Nov 6 '15 at 6:30



















  • What happens if instead of first unwrapping with guard, you skip it and just try to compare state! == MyEnum.FirstCase?

    – Nicolas Miari
    Nov 6 '15 at 6:21











  • @NicolasMiari your code runs fine here as long as you initialise state property

    – Leo Dabus
    Nov 6 '15 at 6:23











  • I repeat: the compiler (static analyzer?) has no way to know if the property is initialized or not at the location where it is used (viewDidLoad), and I am force-unwrapping it (which I souldn't need to, since it is an implicitly unwrapped optional to begin with). And the compiler error I was getting had nothing to do with initialization, but with comparing two enums of the same type (?????)

    – Nicolas Miari
    Nov 6 '15 at 6:30

















What happens if instead of first unwrapping with guard, you skip it and just try to compare state! == MyEnum.FirstCase?

– Nicolas Miari
Nov 6 '15 at 6:21





What happens if instead of first unwrapping with guard, you skip it and just try to compare state! == MyEnum.FirstCase?

– Nicolas Miari
Nov 6 '15 at 6:21













@NicolasMiari your code runs fine here as long as you initialise state property

– Leo Dabus
Nov 6 '15 at 6:23





@NicolasMiari your code runs fine here as long as you initialise state property

– Leo Dabus
Nov 6 '15 at 6:23













I repeat: the compiler (static analyzer?) has no way to know if the property is initialized or not at the location where it is used (viewDidLoad), and I am force-unwrapping it (which I souldn't need to, since it is an implicitly unwrapped optional to begin with). And the compiler error I was getting had nothing to do with initialization, but with comparing two enums of the same type (?????)

– Nicolas Miari
Nov 6 '15 at 6:30





I repeat: the compiler (static analyzer?) has no way to know if the property is initialized or not at the location where it is used (viewDidLoad), and I am force-unwrapping it (which I souldn't need to, since it is an implicitly unwrapped optional to begin with). And the compiler error I was getting had nothing to do with initialization, but with comparing two enums of the same type (?????)

– Nicolas Miari
Nov 6 '15 at 6:30


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33559936%2fcompiler-error-when-comparing-values-of-enum-type-with-associated-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen