I create a enum like the following and I try 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 the situation I use like this. The bvalue.description() gives me the result "2".
if let bvalue = Test.fromRaw(2) {
bvalue.description()
}
But when I try to use it without If statement, like the following. It gives me a wrong notification on the second line said "Invalid use of '()' to call a value of a non-function type String."
let bvalue = Test.fromRaw(2)
bvalue.description()
I was confused. What is the difference within If or without If statement? Why the second way cannot work? What type is this fromRaw() function returned?
Note that fromRaw() no longer exists. The new usage would be:
Test(rawValue: 2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With