logo
Tags down

shadow

Kotlin: Omitting enum name when its unambiguous


By : user6111993
Date : July 31 2020, 06:00 AM
it should still fix some issue With Swift enums you can omit the name of the enum in cases where only a value of that type can be used. , Just use import, so you can use enum values without enum name
code :
  import CompassPoint.*


Share : facebook icon twitter icon

Shortening or omitting namespace for enum in OData filter


By : user3854318
Date : March 29 2020, 07:55 AM
Hope this helps Web API OData supports the enum prefix free. I think that's you're looking for.
With enum prefix free enabled, you can directly use the enum value, for example, 'MyValue'

How to write a reusable transform for String to Enum value across a group of Enum classes? (Kotlin)


By : Dalton Lanning
Date : March 29 2020, 07:55 AM
will help you I have a group >5 of Enum classes that take String parameter in its values, and I want to have simple code for all these Enum classes to convert from a String field in JSON object. , Enums as other classes can implement interfaces like so:
code :
interface IJsonStringConvertible {
    val jsonStr:String
}

enum class Religiousness(override val jsonStr: String, val resID: Int) : IJsonStringConvertible {
    NotAtAll("none", R.string.not_religious),
    Somewhat("somewhat", R.string.somewhat_religious),
    Very("very", R.string.very_religious),
    ;

    override fun toString() = jsonStr
    fun displayString(res: Resources) = res.getString(resID)
}
for (enumval in enumClass.enumConstants) {
    if ((enumval as IJsonStringConvertible).jsonStr == str)
        return enumval
}

How to get enum value of raw type from an enum class and a string in kotlin


By : Pascal Huynh
Date : March 29 2020, 07:55 AM
I wish this help you I have the following code in java: , Here's a pure Kotlin version:
code :
@Suppress("UNCHECKED_CAST")
fun getEnumValue(enumClass: Class<*>, value: String): Enum<*> {
    val enumConstants = enumClass.enumConstants as Array<out Enum<*>>
    return enumConstants.first { it.name == value }
}
private enum class Hack

fun getEnumValue(enumClass: Class<*>, value: String): Enum<*> {
    return helper<Hack>(enumClass, value)
}

private fun <T : Enum<T>>helper(enumClass: Class<*>, value: String): Enum<*> {
    return java.lang.Enum.valueOf(enumClass as Class<T>, value)
}
enum class Color {
    Red, Green, Blue
}

enumValueOf<Color>("Red")

mapping a graphql enum to a kotlin enum


By : awesumnitz
Date : March 29 2020, 07:55 AM
seems to work fine Ok, so after I ran amplify codegen on my project I had a type.Vegan and type.GlutenFree enums I could use, so I got rid of my local kotlin enums and made the properties of the Ingredient object type.Vegan and type.GlutenFree.
Works like a charm.

Access enum value inside a method of enum class in Kotlin


By : Dvirus
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You can use this and this.ordinal which returns the ordinal of this enumeration constant
Also if you do this:
code :
fun myFunc(): Any{
    val array = MyEnum.values()
    println(array[this.ordinal])
    println(this)
    return array[this.ordinal]
}
shadow
Privacy Policy - Terms - Contact Us © voile276.org