0

What logic is used to create class and protocol names when Kotlin code is exported to Objective-C? Additionally, where can I find detailed information about this process?

Where code like:

package kotlinx.serialization.modules
   
public interface SerializersModuleCollector {...}

from: core/commonMain/src/kotlinx/serialization/modules/SerializersModuleCollector.kt

Is transformed into:

Kotlinx_serialization_coreSerializersModuleCollector
Kotlinx_serialization_coreKSerializer
Kotlinx_serialization_coreSerializationStrategy
...

Full Example:

public protocol Kotlinx_serialization_coreSerializersModuleCollector {

  func contextual(kClass: any KotlinKClass, provider: @escaping ([any Kotlinx_serialization_coreKSerializer]) -> any Kotlinx_serialization_coreKSerializer)

  func contextual(kClass: any KotlinKClass, serializer: any Kotlinx_serialization_coreKSerializer)

  func polymorphic(baseClass: any KotlinKClass, actualClass: any KotlinKClass, actualSerializer: any Kotlinx_serialization_coreKSerializer)

  @available(*, deprecated, message: "Deprecated in favor of function with more precise name: polymorphicDefaultDeserializer")
  func polymorphicDefault(baseClass: any KotlinKClass, defaultDeserializerProvider: @escaping (String?) -> (any Kotlinx_serialization_coreDeserializationStrategy)?)

  func polymorphicDefaultDeserializer(baseClass: any KotlinKClass, defaultDeserializerProvider: @escaping (String?) -> (any Kotlinx_serialization_coreDeserializationStrategy)?)

  func polymorphicDefaultSerializer(baseClass: any KotlinKClass, defaultSerializerProvider: @escaping (Any) -> (any Kotlinx_serialization_coreSerializationStrategy)?)
}

1 Answer 1

0

I've tested with the following structure (shared implements shared-ui-models):

enter image description here
enter image description here enter image description here

And this Model.kt file:

package com.sample.models

import kotlin.experimental.ExperimentalObjCName
import kotlin.native.ObjCName

@OptIn(ExperimentalObjCName::class)
@ObjCName(swiftName = "MyTestScreen")
data class Screen(val name: String)

The output is:

__attribute__((swift_name("Shared_ui_modelsMyTestScreen")))

Or just Shared_ui_modelsScreen if I remove the @ObjCName.

I'm inclined to assume that the logic is:
Module name capitalised + "-" replaced by "_" + class/protocol name.

I would like to get some confirmation on this because when applying this logic to the SerializersModuleCollector.kt file in the OP, we see that its module is simply core. However, the result is Kotlinx_serialization_coreSerializersModuleCollector, which leads me to wonder where the Kotlinx_serialization part comes from.

Not the answer you're looking for? Browse other questions tagged or ask your own question.