https://kotlinlang.org logo
Title
m

Matthew Murray

06/21/2022, 6:54 PM
Hi guys, I’m having trouble doing a
switch
statement with Kotlin/Native enums in Objective-C. I’m getting the error
Statement requires expression of integer type ('NtvCoreRateType *' invalid)
. It seems that Kotlin enum classes don’t get converted into proper enums in objective-C, so I’m wondering if its at all possible to do a switch statement with them? And if not, what is the expected way to compare them?
n

Nikolay Kasyanov

06/22/2022, 9:01 AM
In Swift it’s sorta possible to switch over enum classes (albeit it won’t be exhaustive, requiring a
default:
clause), but in Obj-C, as far as my understanding goes, you’d have to resort to if-else if…. There’s a ticket to expose “raw” enum values to Obj-C/Swift though: https://youtrack.jetbrains.com/issue/KT-48068/Native-Feature-Request-Add-typed-ordinal-for-enums-bridged-to-Obj-C Hopefully it will get some love one day.
m

Matthew Murray

06/22/2022, 7:25 PM
Ah, as I feared. Not the worse problem in the world. Thanks for response!
n

Nikolay Kasyanov

06/22/2022, 7:28 PM
You're welcome! On the second thought, the ordinal property mentioned in the ticket might do the trick for you.
m

Matthew Murray

06/22/2022, 7:33 PM
ya, I tried that too. It didn’t work since for the
case
statements though, since it wants the values to be constants, and it doesn’t see recognize the values from KotlinEnum as constants