Skip to content

Instantly share code, notes, and snippets.

@nunogoncalves
Last active November 3, 2019 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nunogoncalves/c9ee67bb4661e43dd3d4c2440a7097bd to your computer and use it in GitHub Desktop.
Save nunogoncalves/c9ee67bb4661e43dd3d4c2440a7097bd to your computer and use it in GitHub Desktop.
Workaround to have kind of multiple values enums
//: Playground - noun: a place where people can play
import UIKit
enum VehicleType : RawRepresentable {
struct Vehicle : Equatable {
let name: String
let wheels: Int
static func ==(l: Vehicle, r: Vehicle) -> Bool {
return l.name == r.name && l.wheels == r.wheels
}
static var bike: Vehicle {
return Vehicle(name: "Bicycle", wheels: 2)
}
static var car: Vehicle {
return Vehicle(name: "Automobile", wheels: 4)
}
static var bus: Vehicle {
return Vehicle(name: "Autobus", wheels: 8)
}
}
typealias RawValue = Vehicle
case car
case bus
case bike
var rawValue: RawValue {
switch self {
case .car:
return Vehicle.car
case .bike:
return Vehicle.bike
case .bus:
return Vehicle.bus
}
}
init?(rawValue: RawValue) {
switch rawValue {
case Vehicle.bike:
self = .bike
case Vehicle.car:
self = .car
case Vehicle.bus:
self = .bus
default: return nil
}
}
}
VehicleType.bike.rawValue.name
VehicleType.bike.rawValue.wheels
VehicleType.car.rawValue.wheels
VehicleType(rawValue: .bike)?.rawValue.name
VehicleType(rawValue: .bike)?.rawValue.wheels
VehicleType(rawValue: .car)?.rawValue.name
VehicleType(rawValue: .car)?.rawValue.wheels
VehicleType(rawValue: .bus)?.rawValue.name
VehicleType(rawValue: .bus)?.rawValue.wheels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment