How can I use the fromRaw() function of enum

Ellie

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?

Nate Cook

The .fromRaw() method returns an optional value of your enumeration. So instead of bvalue having type Test, it has type Test?. In order to access its value you need to either unwrap it with an if let... like your example or force the unwrapping with the ! operator:

bvalue!.description()

Note that if the .fromRaw() call failed, you'll get a runtime error. Try this to see it:

let bvalue = Test.fromRaw(5)   // 5 is out of bounds, bvalue == nil
bvalue!.description()          // runtime error: Can't unwrap Optional.None

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I use an Enum for Validation in VBA

From Dev

How can I use an Enum for Validation in VBA

From Dev

Why does fromRaw() return nil when I use a variable as an argument?

From Dev

Can I use an enum within an enum?

From Dev

How would I use an enum contained in a struct as a parameter for a function?

From Dev

How can I add the value of a Typescript enum to my javascript function?

From Dev

How can I display the function that returns enum type data in this program

From Dev

How can I call an overloaded function based on enum typeinfo?

From Dev

How can I display the function that returns enum type data in this program

From Dev

How can I use my enum in QString.arg()?

From Dev

How can I use a Swift enum as a Dictionary key? (Conforming to Equatable)

From Dev

How can I use my enum in QString.arg()?

From Dev

How can i use enum to pass variables between two scripts?

From Dev

How can I use indices in the subset function

From Dev

How can I use a function variable in a .then?

From Dev

How can I use an extended jQuery function?

From Dev

How can I use an alias in a function?

From Dev

How can I use function pointers in Nimrod?

From Dev

How can I use splatting in a parameterized function?

From Dev

how can i use a const function with "this"

From Dev

How can I use other function variables?

From Dev

How can I use the prepend() function?

From Dev

How can I use polymorphism with std::function?

From Dev

how can I use void** function(void**)

From Dev

How can I use ddply inside a function?

From Dev

How can I use an alias in a function?

From Dev

How can I use a function variable in a .then?

From Dev

How can I use readbyte function faster

From Dev

How can I use the prepend() function?

Related Related

  1. 1

    How can I use an Enum for Validation in VBA

  2. 2

    How can I use an Enum for Validation in VBA

  3. 3

    Why does fromRaw() return nil when I use a variable as an argument?

  4. 4

    Can I use an enum within an enum?

  5. 5

    How would I use an enum contained in a struct as a parameter for a function?

  6. 6

    How can I add the value of a Typescript enum to my javascript function?

  7. 7

    How can I display the function that returns enum type data in this program

  8. 8

    How can I call an overloaded function based on enum typeinfo?

  9. 9

    How can I display the function that returns enum type data in this program

  10. 10

    How can I use my enum in QString.arg()?

  11. 11

    How can I use a Swift enum as a Dictionary key? (Conforming to Equatable)

  12. 12

    How can I use my enum in QString.arg()?

  13. 13

    How can i use enum to pass variables between two scripts?

  14. 14

    How can I use indices in the subset function

  15. 15

    How can I use a function variable in a .then?

  16. 16

    How can I use an extended jQuery function?

  17. 17

    How can I use an alias in a function?

  18. 18

    How can I use function pointers in Nimrod?

  19. 19

    How can I use splatting in a parameterized function?

  20. 20

    how can i use a const function with "this"

  21. 21

    How can I use other function variables?

  22. 22

    How can I use the prepend() function?

  23. 23

    How can I use polymorphism with std::function?

  24. 24

    how can I use void** function(void**)

  25. 25

    How can I use ddply inside a function?

  26. 26

    How can I use an alias in a function?

  27. 27

    How can I use a function variable in a .then?

  28. 28

    How can I use readbyte function faster

  29. 29

    How can I use the prepend() function?

HotTag

Archive