Quantcast
Channel: How do I create an enum from an Int in Kotlin? - Stack Overflow
Viewing all articles
Browse latest Browse all 13

Answer by Francesc for How do I create an enum from an Int in Kotlin?

$
0
0
enum class Types(val value: Int) {    FOO(1),    BAR(2),    FOO_BAR(3);    companion object {        fun fromInt(value: Int) = Types.values().first { it.value == value }    }}

You may want to add a safety check for the range and return null.


Viewing all articles
Browse latest Browse all 13

Trending Articles





<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>