How can i get an enum value from a string? i only ...
# getting-started
j
How can i get an enum value from a string? i only have a kclass, so not the concrete enum class
d
I typically write a
fromString(someString: String): YourEnum
function for that kind of behavior. I don't know of a built in
String -> Enum
converter.
h
The built in
String -> Enum
is called
valueOf
(a synthethic method, just like in Java). Example: https://pl.kotl.in/Ucxx-Y8cd
However, for this you need the actual Enum, not just its
KClass
. 🤔
j
i now use :
Copy code
val theClass = current.javaClass
                return theClass.enumConstants.first { it.name ==  name }
s
Why dont you just use javas:
Copy code
java.lang.Enum.valueOf(Foo::class.java, strValue)
j
right even better