Kotlin Multiplatform, from the compiler up · Puzzle 5 · 6 of 16

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 packs native code into a framework; the framework includes an Objective-C header describing the API; the Swift app reads the header and calls the code Kotlin/Nativenative code Shared.framework native code Objective-C header Swift appreads
  1. Kotlin/Native has compiled your shared code to native code. But it's only CPU instructions: no names, no types.

    Step 1 of 4
  2. So 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 4
  3. The 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 4
  4. The 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

The iOS team opens your framework in Xcode. What do they read to learn your API?

Not quite. Kotlin describes your code in Objective-C, not Swift; Swift reads that.

Not quite. The framework holds native code and an Objective-C description; Swift never reads Kotlin.

Right. Swift learns your API from the Objective-C header.

Why does Kotlin describe your code in Objective-C, not in Swift?

Right. Objective-C is the language Swift already knows how to call.

Not quite. Swift runs native code fine; it needs a description it can read, and Objective-C is one. Re-read that part ↑

What's the difference between a framework and a header?

Not quite. It's the other way round: the header lists, the framework holds the code too.

Not quite. A framework contains the header, plus the compiled code the header describes.

Right. Package vs its description.

Sources

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.