I have my enum as below
enum class ImagesType(val descriptor: String) {
BIGGER("Bigger Image - Fall"),
SMALLER("Smaller Image - Lion"),
TALLER("Taller Image - Tree"),
LONGER("longer Image - Bridge")
}
println(ImagesType.BIGGER) it will print BIGGERval x = ImagesType.valueOf("SMALLER"), it will get val x = SMALLERThis is because the name is the same as the enum characters. I am hoping to override the name with the description, where the following will be true instead
println(ImagesType.BIGGER) it will print Bigger Image - Fallval x = ImagesType.valueOf("Smaller Image - Lion"), it will get val x = SMALLERI tried
enum class ImagesType(override val name: String) {
BIGGER("Bigger Image - Fall"),
SMALLER("Smaller Image - Lion"),
TALLER("Taller Image - Tree"),
LONGER("longer Image - Bridge")
}
But it fails stating that name is final.
I use way to get what I wanted.
enum class ImagesType(val descriptor: String) {
BIGGER("Bigger Image - Fall"),
SMALLER("Smaller Image - Lion"),
TALLER("Taller Image - Tree"),
LONGER("longer Image - Bridge");
override fun toString(): String {
return descriptor
}
companion object {
fun getEnum(value: String): ImagesType {
return values().first { it.descriptor == value }
}
}
}
So it get the result I need
println(ImagesType.BIGGER) it will print Bigger Image - Fallx = ImagesType.getEnum("Smaller Image - Lion"), it will get val x = SMALLERI kind of workaround of overriding valueOf by replacing it with getEnum. Looks like valueOf can't be overriden.
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