Is there a way to dump the rawValue enum to its type?

I have the following enum:

enum Difficulty: Int {
    case Opt1 = 1, Opt2 = 2, Opt3 = 3    
}

      

When I try to use it as an Int like this: MyClass.Difficulty.Opt1.rawValue

it doesn't let me because it returns RawValue. Shouldn't it be wise to understand that this is Int?

+3


source to share


2 answers


Given your code, I assume you have an enum nested inside a class like this:

class MyClass {
    enum Difficulty: Int {
        case Opt1 = 1, Opt2 = 2, Opt3 = 3
    }
}

var myInteger = MyClass.Difficulty.Opt2.rawValue // Int inferred correctly

      



I don't find any problem with this or any options I can think of. Please write your code for the MyClass type if you are still facing problems.

+2


source


If you want to pass it to a function:

func someFunction(difficulty: MyClass.Difficulty) {
    let number = difficulty.rawValue 
}

someFunction(.Opt1)

      



or

fun someFunction(difficulty: Int) {
    let number = difficulty
}

someFunction(MyClass.Difficulty.Opt1.rawValue)

      

0


source







All Articles