Kotlin Multiplatform, from the compiler up · Puzzle 3 · 4 of 10
How Swift sees your Kotlin
In puzzle 0, Kotlin/Native turned your code into native code for the iPhone. But the iOS app is written in Swift. Here's how Swift finds and calls what you built.
The problem
In puzzle 0, Kotlin/Native compiled your shared code to native code for the iPhone. But the iOS app is written in Swift, by a team that may never open a Kotlin file. Native code is just instructions for a CPU: it has no names, no types, no "here's a class called LoginRules". How does Swift know what's in there, and how to call it?
See it
Kotlin/Native has compiled your shared code to native code. But it's only CPU instructions: no names, no types.
Step 1 of 4So it's packed into a framework, Apple's format for a library: the compiled code plus a description of what's inside.
Step 2 of 4The description is a header, written in Objective-C, the older language Swift can call directly. Predict: what does the Swift app read?
Step 3 of 4The header. Swift sees your Kotlin classes as Objective-C classes, and calls the native code behind them.
Step 4 of 4
The building block
For iOS, Kotlin/Native packs your shared code into a framework: Apple's format for a library. A framework holds two things: the compiled code, and a description of what's inside, so other code knows what it can call.
That description is a header: a file that lists every class, function and type, without their code. Kotlin writes it in Objective-C, the older language of Apple's platforms, because Swift can call Objective-C directly. Swift has no direct line to Kotlin; it goes through that Objective-C description.
A Gradle task builds the framework (it takes its name from your shared module, often called shared), and the iOS app's project in Xcode, Apple's counterpart of Android Studio, links it in. So the iOS team imports the framework, and in Swift your LoginRules class looks like any other Objective-C class: Swift reads the header and calls the native code behind it.
That middle step has a price. Objective-C is older and simpler than both Kotlin and Swift, so some things don't survive the trip. The next puzzle shows which.
The analogy
A book translated into a third language before it reaches its readers. The readers never see the original; they read the translation. Whatever that third language has no words for gets flattened on the way.
Check yourself
Sources
- Kotlin docs: Interoperability with Swift/Objective-C ("Kotlin/Native provides indirect interoperability with Swift through Objective-C. This document covers how you can use Kotlin declarations in Swift/Objective-C code")
- Kotlin docs: The basics of Kotlin Multiplatform project structure ("When it's built into an iOS framework, common Kotlin is treated as Kotlin/Native")
What you now know
Kotlin/Native packs your shared code into a framework, Apple's format for a library. Its description is an Objective-C header, because Swift can call Objective-C directly; Swift reads that header and calls your Kotlin as if it were Objective-C.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.