In Swift 2, can you create an enum from a string?
enum Food : Int { case Pizza, Pancakes }
let str = "Pizza"
let food = Food(name:str) // for example
That last line doesn't work, but I'm looking for something like it. Like in Java, you can say Food.valueOf("Pizza")
.
Edit: I can't use a String as a raw value.
You can create an initializer for the enum that takes a
String
as a parameter. From there, you switch over the string to set the value of self to a specific case like so: