I have a function declared as (in KMM) : ```fun ge...
# multiplatform
p
I have a function declared as (in KMM) :
Copy code
fun get(): Any?
In my iOS implementation I have:
*func* get() -> *Any*? {
*return* Int(0)
}
Which I call from KMM as:
Copy code
val myValue = myImplementation.get() as Int
and I am getting the following Exception:
kotlin.ClassCastException: class kotlin.Long cannot be cast to class <http://kotlin.Int|kotlin.Int>
Not sure why this might happen. Any comments/ideas are welcome. Thanks
t
Int is 64 bit when compiling for a 64 bit target in Swift, so it maps to Long in Kotlin
c
You shouldn’t cast primitives in Kotlin. Use the extension functions instead (
.toInt()
,
.toLong()
,
.toDouble()
, etc)
p
@Tijl Thanks for info. Do you know if this is documented somewhere?
@Casey Brooks OK, but my interface returns Any as the type is not known at this point. I can't really use toInt() as this is not available on Any. Casting to Int is my only option.
I think I read the original question a bit too quickly. It looks like this is a situation of the platform Number representations not playing well with Kotlin. I’m not too familiar with Swift/obj-c, but it is documented that Swift/obj-c primitives generally don’t have enough info to the Kotlin runtime to really handle them very well https://kotlinlang.org/docs/native-objc-interop.html#nsnumber So in this case you’d probably have to cast to Long, and then use
.toInt()
on that result.
(myImplementation.get() as Long).toInt()
p
I was looking for documentation on Kotlin Multiplatform mapping Int to Long on 64-bit system (on Android this work as expected). I understand that casting it first to a Long is a workaround for me. Thanks