Swift enum inheritance

Boon picture Boon · Oct 17, 2015 · Viewed 43.4k times · Source

Can you inherit enum in Swift? What are the rules that one should be aware of with regards to enum inheritance?

The following test code:

enum TemperatureUnit: Int {
    case Kelvin, Celcius, Farenheit
}

enum TemperatureSubunit : Temperature {  
}

generates

error: type 'TemperatureSubunit' does not conform to protocol 'RawRepresentable'

Answer

Korpel picture Korpel · Oct 17, 2015

In Swift language, we have Structs, Enum and Classes. Struct and Enum are passed by copy but Classes are passed by reference. Only Classes support inheritance, Enum and Struct don't.

So to answer your question, you can't have inheritance with Enum (and Struct types). Have a look here:

stackOverflow difference classes vs structs