https://kotlinlang.org logo
j

James Black

04/13/2022, 7:02 PM
In my shared subproject I have this enum. I would like to loop over it in Swift to have toggles.
Copy code
enum class ClothingTypes(val clothingLabel:String) {
    BLAZER("blazer"),
    BLOUSE("blouse"),
    UNDERSHIRT("undershirt");

    companion object {
        fun getNumberOfItems() = values().size
    }
}
This code isn't going to compile but basically how would I loop over ClothTypes and get each item in the enum? I did this on the Android side already. ClothingTypes.allCases.forEach { item in Toggle(isOn: $isPushEnable) { item.text } } This is basically what I want to do in a loop. Toggle(isOn: $isPushEnable) { Text(list.*get*(index: 0)!.clothingLabel) } Toggle(isOn: $isPushEnable) { Text(list.*get*(index: 1)!.clothingLabel) }
I solved it by doing:
Copy code
val clothingTypeList = ClothingTypes.values().map{it.clothingLabel}
in my shared viewmodel and then exposing that in the iosApp view model (it is basically just a pass-thru) and I can now use clothingTypeList to loop over.
5 Views