Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the fromRaw() function of enum

Tags:

swift

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?

like image 633
Ellie Avatar asked Jan 11 '23 11:01

Ellie


1 Answers

Note that fromRaw() no longer exists. The new usage would be:

Test(rawValue: 2)
like image 148
ZaBlanc Avatar answered Jan 16 '23 20:01

ZaBlanc