0Pricing
Kotlin Multiplatform Academy · درس

الحصول على اسم المنصة لكل هدف

دالة expect/actual تقليدية لنظامَي Android وiOS

الحصول على اسم المنصة لكل هدف درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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(): String

The 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_INT

The 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.systemName

Signatures 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. 🎉

الأسئلة الشائعة

هل درس «الحصول على اسم المنصة لكل هدف» مجاني؟

نعم — نص درس «الحصول على اسم المنصة لكل هدف» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

ماذا ستتعلم في «الحصول على اسم المنصة لكل هدف»؟

دالة expect/actual تقليدية لنظامَي Android وiOS تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟

لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «الحصول على اسم المنصة لكل هدف»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟

نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. لماذا توجد expect/actual
  2. الحصول على اسم المنصة لكل هدف
  3. فئات وخصائص expect/actual
  4. الأخطاء الشائعة وأخطاء المترجم
← العودة إلى Kotlin Multiplatform Academy