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

Bao Le Duc

03/21/2023, 1:02 PM
Hi, We have a naming conflict on Swift code which use our Kmp library. Is there a way to prefix all class names which generated by Kmp for iOS/Swift?
j

John O'Reilly

03/21/2023, 1:14 PM
I think you should be able to use
ObjCName
for that - https://kotlinlang.org/docs/native-objc-interop.html#name-translation
b

Bao Le Duc

03/21/2023, 1:15 PM
ObjCName
can help to transform individual classes, I would do it for every classes in my library
j

Jeff Lockhart

03/21/2023, 2:51 PM
Kotlin types are prefixed in Objective-C with a prefix derived from the framework name. But in Swift, the prefix is removed, e.g. (for a framework Foo):
Copy code
__attribute__((swift_name("Bar")))
@interface FOOBar
Because Swift has module namespacing, ObjC's convention for type prefixing is optional in Swift. If you run into a situation where two modules used in the same file have conflicting types, you can use a local typealias to disambiguate, e.g.:
Copy code
typealias AMyClass = ModuleA.MyClass
typealias BMyClass = ModuleB.MyClass
b

Bao Le Duc

03/21/2023, 2:54 PM
That would be an option, but we would like to have the same prefix for every classes from the Kmp library in Swift. It’s easier to keep track
j

Jeff Lockhart

03/21/2023, 3:00 PM
I don't believe there's an option to globally prefix all Kotlin types in Swift.
b

Bao Le Duc

03/21/2023, 3:03 PM
We need to use either
ObjCName
annotation and/or Swift’s typealias then. Thanks @Jeff Lockhart, your tips is really helpful
15 Views