Her Hedef için Platform Adını Alma
Android ve iOS için klasik bir expect/actual işlevi oluşturun.
Her Hedef için Platform Adını Alma, CoddyKit'te ücretsiz bir Kotlin Multiplatform Academy dersidir. Bu, 4 dersinin 2. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Kotlin Multiplatform Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Kotlin Multiplatform Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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. 🎉
Sıkça Sorulan Sorular
“Her Hedef için Platform Adını Alma” dersi ücretsiz mi?
Evet — “Her Hedef için Platform Adını Alma” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Kotlin Multiplatform Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Kotlin Multiplatform Academy kursu toplamda 4 dersten oluşur.
“Her Hedef için Platform Adını Alma” dersinde ne öğreneceğim?
Android ve iOS için klasik bir expect/actual işlevi oluşturun. Kotlin Multiplatform Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Kotlin Multiplatform Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Kotlin Multiplatform Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 2. dersidir.
“Her Hedef için Platform Adını Alma” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Kotlin Multiplatform Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Kotlin Multiplatform Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- expect/actual Neden Var
- Her Hedef için Platform Adını Alma
- expect/actual Sınıfları ve Özellikleri
- Yaygın Hatalar ve Derleyici Hataları