รับชื่อแพลตฟอร์มสำหรับแต่ละเป้าหมาย
ฟังก์ชัน expect/actual แบบคลาสสิกสำหรับ Android และ iOS
รับชื่อแพลตฟอร์มสำหรับแต่ละเป้าหมาย เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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(): 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. 🎉
คำถามที่พบบ่อย
บทเรียน “รับชื่อแพลตฟอร์มสำหรับแต่ละเป้าหมาย” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “รับชื่อแพลตฟอร์มสำหรับแต่ละเป้าหมาย” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “รับชื่อแพลตฟอร์มสำหรับแต่ละเป้าหมาย”
ฟังก์ชัน expect/actual แบบคลาสสิกสำหรับ Android และ iOS คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “รับชื่อแพลตฟอร์มสำหรับแต่ละเป้าหมาย” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม
ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุผลที่มี expect/actual
- รับชื่อแพลตฟอร์มสำหรับแต่ละเป้าหมาย
- คลาสและพร็อพเพอร์ตี expect/actual
- ข้อผิดพลาดทั่วไปและข้อผิดพลาดของคอมไพเลอร์