// SI units
enum Magnitude : Measurement {
case Milli = Measurement(-3, \"ml\")
case Centi = Measurement(-2, \"cl\")
case Desi = Measurement(-1, \"dl\")
Did you consider using a dictionary? For example:
let magnitudes: [String: Double] = [
"ml": -3,
"cl": -2,
"dl": -1,
"g": 0,
"kg": 3
]
// list all units:
println(", ".join(magnitudes.keys)) // kg, g, ml, cl, dl
// convert
func convert(from: String, to: String, value: Double) -> Double{
let mag1 = __exp10(magnitudes[from]!)
let mag2 = __exp10(magnitudes[to]!)
return value * mag1 / mag2
}
let result = convert("dl", "ml", 3.0)
println(result) // 300.0