Enums that depend on the value of another Enum

Let’s say I have a constant with an Enum type. Based on the value of that constant, I want to restrict the Enum type of another constant.

Example: I have a vehicle. Depending on whether that vehicle is human-powered or motorized, I want to say what specific kind of vehicle I have.

enum VehicleType {
    
    enum HumanPoweredVehicle {
        case bicycle
        case skateboard
        case pogoStick
    }

    enum MotorizedVehicle {
        case car
        case truck
        case motorcycle
    }

    case humanPowered
    case motorized
    
}

struct Vehicle {
    var vehicleType: VehicleType
    var specificVehicle: // ??? what do I put here
}

var v: Vehicle

So if v.vehicleType = .humanPowered I want v.specificVehicle to be restricted to values from the VehicleType.HumanPoweredVehicle enum. But if it’s .motorized then I would want it to be a value from the VehicleType.MotorizedVehicle enum.

How would I do that?

hello your enum looks a bit weird i am not sure if it will work

maybe take at look at a enum with associated values where you can use different values for each attribute

i made a simple article about enums here http://bit.ly/2n402ft

1 Like