'Using an int as a case in an enum
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 1:[1]
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
Solution 2:[2]
Raw values of an enum is put on the right side of the =. In your enum Temperature, the raw value type is Int, so you should probably do it this way:
enum Temperature: Int {
case Zero = 0, One, Two, Three
}
I didn't write the raw values for the other cases because they can be inferred.
Now you can access a case like this:
Temperature.One
"But I want to access it using integer literals!" you cried.
Unfortunately, this is impossible in Swift. The closest thing you can get is this:
enum Temperature: Int {
case _0 = 0, _1, _2, _3
}
And you can initialize your enum using the initializer:
Temperature(rawValue: 1)
Solution 3:[3]
Best option I would say that you use a dictionary:
let Temperatures:[Int: String] = [0: "zero", 1:"one"]
Then you can access it with the slider value. Be aware of when accessing values not in dictionary -> Check for nil.
Solution 4:[4]
Use tuple, enum is not for this purpose.
let temperature = ("Zero", "One", "Two", "Three")
let zero = temperature.0
Solution 5:[5]
you can try this way
enum CompassPoint : Int {
case north = 0
case south = 1
case east = 2
case west = 3
}
func checkPoint(compass : CompassPoint) -> String {
switch compass {
case .east:
return "Batsman"
case .south:
return "Bowler"
case .north:
return "Wicket Keeper"
case .west:
return "All Rounder"
}}
print(checkPoint(compass: CompassPoint(rawValue: 3)!))
Solution 6:[6]
I found your question while seeking the same answer, but I solved it myself with the CaseIterable and RawRepresentable Protocols. Going back to the OP's example:
enum Temperatures: String, CaseIterable, RawRepresentable {
case zero
case one
case two
case three
}
You can then implicitly reference the cases (as Strings) as indexes of the enum like so:
let x = Temperatures.allCases[0].rawValue
// x = "zero"
let y = Temperatures.allCases[2].rawValue
// y = "two"
For capitalization, you can either define the cases as capitals (which is not considered proper code)...
//...
case Zero
case One
//...
or explicitly define the RawRepresentable String values for each case:
//...
case zero = "Zero"
case one = "One"
//...
or go back to my first example and simply .capitalized the implict String values:
//...
case zero
case one
//...
let x = Temperatures.allCases[0].rawValue.capitalized
// x = "Zero"
These Protocols have come in SO handy for me! Great for iterating Enums:
for temp in Temperatures.allCases {
print(temp.rawValue.capitalized)
}
/* Output
Zero
One
Two
Three
*/
I was really surprised that Enum cases weren't automatically implicitly assigned Int values under the hood (like C#), but the CaseIterable Protocol makes them almost as easily referenced as such.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kametrixom |
| Solution 2 | Jason Moore |
| Solution 3 | Ehab Saifan |
| Solution 4 | Muhammad Raza |
| Solution 5 | kishor soneji |
| Solution 6 | Michael Robinson |
