Calimari328 Calimari328 - 5 months ago 40
Swift Question

Swift enum multiple cases with the same value

In C you could make your enums have this

typedef enum _Bar {
A = 0,
B = 0,
C = 1
} Bar;


In swift I want to make the equivalent. However The compiler complains that it isn't unique. How do I tell it that I want two cases to have the same value?

enum Bar : Int {
case A = 0
case B = 0 //Does not work
case C = 1
}


Ive tried

case A | B = 0


and

case A, B = 0


But it doesn't seem to work as I want it to. I appreciate any help.

Answer

Swift doesn't support duplicated values. (or "aliases" semantically) If you don't mind, you can mimic it by using something like this.

enum Foo: Int {
    case Bar = 0

    static var Baz:Foo {
        get {
            return  Bar
        }
    }
    static var Jar:Foo {
        get {
            return  Foo(rawValue: 0)!
        }
    }
}