I am creating an enumeration as follows, and I am trying to use the fromRaw () function.
enum Test : Int {
case a = 1
case b, c
func description() -> String{
switch self {
case .a:
return "a"
default:
return String(self.toRaw())
}
}
}
It works in a situation that I use like that. Bvalue.description () gives me the result "2".
if let bvalue = Test.fromRaw(2) {
bvalue.description()
}
But when I try to use it without an If statement, for example, the following. This leads me to an incorrect notification on the second line: "Invalid use of" () "to call a non-string value of type String."
let bvalue = Test.fromRaw(2)
bvalue.description()
I was confused. What is the difference inside an if statement or without if? Why can't the second method work? What type does the fromRaw () function return?
Ellie source
share