Kotlin global functions not converted to Swift with KMP

64 Views Asked by At

I'm working on a Kotlin Multiplatform project where I have some Kotlin functions that are not within a class. According to the documentation, these functions should be directly converted to Swift functions accessible from Swift code. However, I'm encountering an issue where these functions are not being converted, would you help me, are there some steps to generate them.

1

There are 1 best solutions below

4
Phil Dukhov On

KMP generates ObjC headers, and since ObjC has no global functions, Kotlin generates a class that contains a static function.

From Swift you can call it like FileNameKt.globalFunction if your globalFunction is placed inside FileName.kt.

In such cases, when you don't know what the generated version looks like in Swift, it is useful to take a look at the generated headers, where you can search for the needed name. You can find it in shared.framework/Headers/shared.h where shared is the name of your KMP framework - I prefer to add it to Xcode source files without adding to any target for easy access.