expect/actual สำหรับข้อมูลอุปกรณ์
ส่งคืนเวอร์ชัน OS และรุ่นอุปกรณ์ของแต่ละแพลตฟอร์ม
expect/actual สำหรับข้อมูลอุปกรณ์ เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Kotlin Multiplatform Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
From Interface to expect
Sometimes you skip an interface and use expect/actual directly. It declares device info once in commonMain and implements it per platform.
Declare the expect Class
In commonMain, write an expect class with the device facts you need. There is no body yet, only the shape both platforms must satisfy.
expect class DeviceInfo() {
val model: String
val osVersion: String
}The Android actual
In androidMain, the actual reads Build.MODEL and the OS release to fill in real Android values at runtime.
actual class DeviceInfo {
actual val model = Build.MODEL
actual val osVersion = Build.VERSION.RELEASE
}The iOS actual
In iosMain, the actual uses UIDevice to report Apple's model name and the running iOS version.
actual class DeviceInfo {
actual val model = UIDevice.currentDevice.model
actual val osVersion = UIDevice.currentDevice.systemVersion
}Signatures Must Line Up
Every property on each actual must match the expect by name and type. A single mismatch and the compiler refuses to link the targets.
Use It From Shared Code
Common code creates DeviceInfo() and reads its properties like normal Kotlin. The correct actual is wired in at build time per target.
fun banner() = "Running on " + DeviceInfo().modelAndroid Gets Build Values
When the Android app builds, the androidMain actual is linked, so model returns the real hardware name from the Build class.
iOS Gets UIDevice Values
The identical call inside the iOS framework returns Apple's values, because the iosMain actual was compiled into that target instead.
Imports Stay In Source Sets
Build lives in androidMain and UIDevice in iosMain. CommonMain never imports a platform type, which is exactly what keeps it shared.
expect/actual vs Interface
An interface needs you to inject an implementation. expect/actual links automatically at compile time, so callers just construct the class.
Pick the Simpler Tool
For a single, stateless platform fact, expect/actual is the leaner choice. Reach for an interface when you also want to swap fakes freely.
Quick Check
Consider how the right device values reach each app.
Recap
You declared an expect DeviceInfo in commonMain and gave Android and iOS actuals, with matching signatures so each app reports its own values. 🎉
คำถามที่พบบ่อย
บทเรียน “expect/actual สำหรับข้อมูลอุปกรณ์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “expect/actual สำหรับข้อมูลอุปกรณ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “expect/actual สำหรับข้อมูลอุปกรณ์”
ส่งคืนเวอร์ชัน OS และรุ่นอุปกรณ์ของแต่ละแพลตฟอร์ม คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “expect/actual สำหรับข้อมูลอุปกรณ์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม
ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ออกแบบอินเทอร์เฟซความสามารถ
- expect/actual สำหรับข้อมูลอุปกรณ์
- ระบบไฟล์และเส้นทางแยกตามแพลตฟอร์ม
- ฉีดการใช้งานของแพลตฟอร์ม