6

I'm trying to iterate over an enum but I cannot access any values they were set with. Android Studio only sees the name and ordinal, but nothing else.

I'm not sure if this is an android studio problem as other online tutorials for kotlin enums seem to do what I do and work just fine.

    enum class TimeStamps(value : Long, text : String) {
        Hour(0, "Past Hour"),
        Day(3600000, "Today"),
        Yesterday(86400000, "Yesterday"),
        Week(172800000, "This week"),
        LastWeek(604800000, "Last week"),
        LastMonth(1209600000, "Last month"),
        LastYear(2628000000, "Last year"),
        LongTime(31540000000, "A long time ago")
    }


TimeStamps.LastMonth.value
TimeStamps.LastMonth.text  //Both of these are said to be undefined by Android Studio

enumValues<TimeStamps>().forEach {

            it.value //Also undefined
}

I'm not exactly sure what is going on. The only error message I get is the Unresolved Reference error. Any help is greatly appreciated, thanks!

1 Answer 1

20

You need to use val if you want it to be a property:

enum class TimeStamps(val value: Long, val text: String) {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.