[SOLVED] Using an int as a case in an enum

Issue

I’m working with some basic Swift operations. Using a slider, I want to dictate a corresponding label. As the slider uses an int to represent the position, I was going to use an enum to convert.

enum Temperature: Int {
   case 0 = "Zero"
   case 1 = "One"
   case 2 = "Two"
   case 3 = "Three"
}

And I would like to call it like this:

variable = Temperature.0

Any help would be ideal. Please let me know your thoughts.

Thanks!

Solution

You can either use an array:

let temperatures = ["Zero", "One", "Two", "Three"]

let one = temeratures[1]

Or a tuple:

let temperatures = ("Zero", "One", "Two", "Three")

let one = temperatures.1

Whereas you cannot use runtime values (non-literals) in the latter

Answered By – Kametrixom

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published.