0Pricing
Kotlin Multiplatform Academy · レッスン

ターゲットごとにプラットフォーム名を取得する

AndroidとiOS向けの典型的なexpect/actual関数を作ります

「ターゲットごとにプラットフォーム名を取得する」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

The Hello World of KMP

Returning the platform name is the classic first expect/actual you write. It proves your shared module really runs on both apps. 👋

Declare the expect

Start in commonMain with an expect function that returns a String. No body yet, just the shape both platforms agree on.

expect fun platform(): String

The Android actual

In androidMain, provide the actual using Android's Build class to report the Android version at runtime.

actual fun platform(): String =
  "Android " + Build.VERSION.SDK_INT

The iOS actual

In iosMain, the actual reaches into Apple's UIKit to read the device's system name and version.

actual fun platform(): String =
  UIDevice.currentDevice.systemName

Signatures Must Match

Both actuals share the exact name, parameters, and return type of the expect. One mismatch and the compiler refuses to build.

Call It from Shared Code

Now any common function can call platform() freely. It reads like normal Kotlin while the right actual runs underneath.

fun greeting() = "Hi from " + platform()

Android Sees Its Value

When the Android app calls greeting(), the compiler linked the androidMain body, so it shows the Android string.

iOS Sees Its Value

The very same call from Swift returns the iOS version instead, because the iosMain actual was compiled into that framework.

Platform Imports Stay Local

Notice that Build and UIDevice are imported only in their own source sets. Shared code never imports a platform type directly.

A Tiny Surface Area

Keep the expect small and focused. One function that returns a name is enough to bridge into each platform cleanly. ✨

Test It Early

Run both apps and print the result. Seeing two different strings confirms your expect/actual wiring works end to end.

Quick Check

Think about how the right value gets chosen.

Recap

You built the classic platform() function: one expect in commonMain, an actual in androidMain and iosMain, and matching signatures across all three. 🎉

よくある質問

「ターゲットごとにプラットフォーム名を取得する」レッスンは無料ですか?

はい。「ターゲットごとにプラットフォーム名を取得する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。

「ターゲットごとにプラットフォーム名を取得する」で何を学びますか?

AndroidとiOS向けの典型的なexpect/actual関数を作ります ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Kotlin Multiplatform Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「ターゲットごとにプラットフォーム名を取得する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?

はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. expect/actualが存在する理由
  2. ターゲットごとにプラットフォーム名を取得する
  3. expect/actualのクラスとプロパティ
  4. よくあるミスとコンパイラーエラー
← Kotlin Multiplatform Academyに戻る