0

I have a simple code like:

enum Coin {
    case heads: 0
    case tails
}

It throws an error on Line 2 that

error: 'case' label can only appear inside a 'switch' statement

How do I solve it?

deadcoder0904
  • 7,232
  • 12
  • 66
  • 163

2 Answers2

2

Try this:

enum Coin: Int {
    case heads = 0
    case tails
}
Sam
  • 956
  • 6
  • 12
  • Yes I got the answer as soon as I posted but no other StackOverflow question had the error before. Also, Google didn't work either. Thank you :) – deadcoder0904 Aug 24 '18 at 06:44
0

Whoops that was simple & I found the answer

First I did:

enum Coin {
    case heads = 0
    case tails
}

Just changed : to = but it threw error

Enum case cannot have a raw value if the enum does not have a raw type

So then I specified the type like:

enum Coin: Int {
    case heads = 0
    case tails
}

And voila it works

deadcoder0904
  • 7,232
  • 12
  • 66
  • 163