is there a way to call top-level functions without...
# ios
a
is there a way to call top-level functions without having the specify the file they are located in? instead of ``MyLibraryUtilsKt.foo()` , I want to call it as
foo()
in swift source: https://kotlinlang.org/docs/native-objc-interop.html#top-level-functions-and-properties
d
Not that I'm aware. The Kotlin top-level function
foo
is translated into an Objective-C class method, where the class is the filename. The generated Objective-C header for the the above example looks something like this:
Copy code
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("MyLibraryUtilsKt")))
@interface MyLibraryUtilsKt : SharedBase
+ (void)foo;
@end
Since
foo
is translated to a class method, it must be called via the class. (It only looks like it gets called via the file name... that's because the file name is used as the class name).
d
In Objective-C, there’s no such thing as a top-level function, which is why it isn’t possible for Kotlin to expose them as such.
j
I'd recommend to use SKIE, amongst other features, one is to solve this: https://skie.touchlab.co/features/global-functions
b
It’s something that I’m attempting/attempted to do by creating a “wrapper” framework for our UIKit (with Compose Multiplatform) im working on for a client. https://github.com/BotStacks/mobile-sdk/pull/49 Just trying to make the xcframework bundle properly now