https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
c

Christopher Mederos

11/20/2023, 6:49 AM
I have a kotlin fun defined in a KMP project with an Int paramter -
Copy code
fun getUser(userId: Int) {}
However, when I try to use this function in swift, I'm getting a type error -
Copy code
Cannot convert value of type 'Int?' to expected argument type 'KotlinInt?'
Presumably I'm missing a step or config here? I'd assume that KMP magic handles the Int implementation in each platform without me having to write any additional mapping?
p

Pablichjenkov

11/20/2023, 1:47 PM
You need to pass the swift type that maps your kotlin type. https://kotlinlang.org/docs/native-objc-interop.html#nsnumber Oh wait, you are using the right type. What if you pass an objective-c integer?
c

Christopher Mederos

11/26/2023, 11:52 PM
I ended up adding a swift Int extension to handle the conversion, as per this article! https://medium.com/cox-auto-tech-blog/navigating-seas-with-kmm-an-ios-developer-story-3791310e39eb
Copy code
extension Int {
    /// converts this Integer to KotlinInt
        func toKotlinInt() -> KotlinInt {
            return KotlinInt(integerLiteral: self)
        }
}