expect/actual لمعلومات الجهاز
أعِد إصدار نظام التشغيل والطراز لكل منصة.
expect/actual لمعلومات الجهاز درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 لمعلومات الجهاز» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «expect/actual لمعلومات الجهاز»؟
أعِد إصدار نظام التشغيل والطراز لكل منصة. تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «expect/actual لمعلومات الجهاز»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تصميم واجهة للقدرات
- expect/actual لمعلومات الجهاز
- نظام الملفات والمسارات لكل منصة
- حقن تطبيقات المنصة