I have a question about enum in Swift.
I declared my enum like this:
enum FirstEnum : CustomStringConvertible {
case VALUE1
case VALUE2
case VALUE3
var description: String {
switch self {
case .VALUE1:
return "First value"
case .VALUE2:
return "Second value"
case .VALUE3:
return "Third value"
}
}
func getFromCode(value:String) -> FirstEnum? {
switch value {
case "v1":
return FirstEnum.VALUE1
case "v2":
return FirstEnum.VALUE2
case "v3" :
return FirstEnum.VALUE3
}
}
I need to get enum from a string (like a dictionary) so I expect this line should work:
let foo = FirstEnum.getFromCode("v1")
But XCode (7) expects a FirstEnum parameter for method getFromCode instead a String as declared in method definition, saying:
Cannot convert value of type "String" to expected argument type "FirstEnum"
Why this?...what I'm doing wrong?