Search code examples
kotlinkotlin-multiplatformkotlin-native

How to subclass SwiftUI classes in kotlin-native


Based on the documentation: Subclassing Swift/Objective-C classes and protocols from Kotlin I am trying to subclass a SwiftUI class in Kotlin native. I found some examples for UiKit but no examples for SiwftUI.

This is what I tried so far:

import platform.SwiftUI.*

actual class TestClass: ObservableObject() {

}

But the import for SwiftUI fails with: Unresolved reference: SwiftUI

(A import for platform.UIKit.* is working)

My question: Is it even possible to import SwiftUi classes? if yes how sould the correct import look like?


Solution

  • Kotlin/Native does not offer interop with pure Swift. Currently it works indirectly with a K\N - Obj-C - Swift interop. Direct interoperability with Swift is on the roadmap, but it's currently paused AFAIK

    From the documentation:

    Kotlin/Native provides bidirectional interoperability with Objective-C" ... "A Swift library can be used in Kotlin code if its API is exported to Objective-C with @objc. Pure Swift modules are not yet supported.

    Since SwiftUI is pure Swift, you can't really import it to Kotlin unfortunately (hopefully yet).