Search code examples
swiftkotlinios-frameworkskotlin-multiplatformkotlin-multiplatform-mobile

Why does property name have an underscore (underbar) in generated iOS Framework using Kotlin Multiplatform?


I have a Kotlin Multiplatform project where I am generating an iOS Framework from Kotlin source code.

One of my Kotlin interfaces has a property defined as such:

interface Presenter {
    val validValues: List<Int>
    ...
}

However, in the generated iOS framework's .h file, this is generated with an underbar "_" (underscore) at the end:

@property (readonly) NSArray<XYZInt *> *validValues_ __attribute__((swift_name("validValues_")));

Why is an underbar "_" (underscore) added to the end of the property name?


Solution

  • Turns out that in my source code, I had another interface being used as a data source which had the same property name defined:

    interface MyModelSpecificDataSource {
        val validValues: List<Int>
        ...
    }
    

    As soon as I changed the definition to a different name, the underbar "_" no longer was added to the end.

    Based on this, given that any class could implement any interface, my assumption is that this is being done to distinguish one definition from another so that they don't clash. Good call from the Kotlin team!