Pobieranie nazwy platformy dla każdego celu
Klasyczna funkcja expect/actual dla Androida i iOS
Pobieranie nazwy platformy dla każdego celu to bezpłatna lekcja Kotlin Multiplatform Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Kotlin Multiplatform Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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(): StringThe 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_INTThe 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.systemNameSignatures 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. 🎉
Często zadawane pytania
Czy lekcja „Pobieranie nazwy platformy dla każdego celu” jest bezpłatna?
Tak — pełny tekst „Pobieranie nazwy platformy dla każdego celu” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Kotlin Multiplatform Academy, przejdź na CoddyKit PRO. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Pobieranie nazwy platformy dla każdego celu”?
Klasyczna funkcja expect/actual dla Androida i iOS Ćwiczysz Kotlin Multiplatform Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Kotlin Multiplatform Academy?
Nie wymagamy żadnego doświadczenia. Kotlin Multiplatform Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Pobieranie nazwy platformy dla każdego celu”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Kotlin Multiplatform Academy?
Tak. Każda lekcja Kotlin Multiplatform Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Dlaczego istnieje expect/actual
- Pobieranie nazwy platformy dla każdego celu
- Klasy i właściwości expect/actual
- Typowe błędy i błędy kompilatora