0

Should I explicitly specify the superclass and the starting value of the enum that should contain integer values starting from 0 in Swift?

enum Foo: Int {
    case Bar
    case Baz
}

Thanks in advance.

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242

1 Answers1

2

That's not exactly a superclass of the enum (although it looks like), but the raw value type.

If you want to assign a value to each enum case, then yes, you have to specify the type, which can be Int, but also String and any other type that can be initialized with a literal.

If the raw value type is any integer type, you can omit the value associated to each case, in that case incremental values will be automatically assigned, starting from 0. Non integer types instead must have a value explicitly specified for each case.

Antonio
  • 71,651
  • 11
  • 148
  • 165
  • But if I don't explicitly specify the type, I don't have the rawValue method, right? – FrozenHeart Dec 01 '14 at 22:21
  • 2
    Yes, that's correct. The only available property is `hashValue`, which apparently is an incremental integer (see [this answer](http://stackoverflow.com/a/27094973/148357)) – Antonio Dec 01 '14 at 22:31