Kotlin Multiplatform Academy · درس

دالة ترحيب في commonMain

كتابة أول دالة Kotlin متعددة المنصات

الدرس 1 من 413 خطوة

دالة ترحيب في commonMain درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Meet commonMain

Code you put in commonMain is shared by every platform. It is the heart of KMP, so this is where your first function will live.

Pure Kotlin Only

Inside commonMain you write pure Kotlin with no Android or iOS imports. If it compiles here, it runs the same on both apps.

Pick a Tiny Goal

Your first shared piece is a small greeting function. It takes a name and returns a friendly line that both apps can show. 👋

Write the Function

Declare it with fun and give it one parameter. This single line of Kotlin is now available to Android and iOS alike.

fun greet(name: String): String {
    return "Hello, " + name + "!"
}

String Templates

Kotlin string templates read cleaner than joining with plus. The dollar sign drops a value straight into the text.

fun greet(name: String): String {
    return "Hello, $name!"
}

Return Types Matter

The : String after the parentheses promises a String comes back. Both platforms rely on that contract being honored.

Keep It Side-Effect Free

A good shared function is pure: same input, same output, no logging or UI. That is exactly what makes it safe everywhere.

Default Arguments

Kotlin lets you set a default value, so callers can skip the argument. Both apps then get a sensible greeting for free.

fun greet(name: String = "there"): String {
    return "Hello, $name!"
}

Where the File Lives

Save it under commonMain/kotlin in your shared module. The folder is the signal to KMP that this code targets every platform.

Top-Level Functions

You did not need a class. A top-level function in a file is perfectly fine and keeps tiny shared utilities simple.

One Source of Truth

This single greet function is the one source of truth. Change the wording once and every platform speaks the same way. ✨

Quick Check

Let us check where shared code belongs.

Recap

You wrote your first shared function in commonMain: pure Kotlin, a clear return type, and one source of truth for both apps. 🎉

البدء مجانًا

تعلم Kotlin مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
30
الدروس
120

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

هل درس «دالة ترحيب في commonMain» مجاني؟

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

ماذا ستتعلم في «دالة ترحيب في commonMain»؟

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

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

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

كم من الوقت يستغرق درس «دالة ترحيب في commonMain»؟

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

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

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

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

  1. دالة ترحيب في commonMain
  2. استدعاء التعليمات البرمجية المشتركة من تطبيق Android
  3. استدعاء التعليمات البرمجية المشتركة من تطبيق iOS
  4. غيّر مرة واحدة وشاهد النتيجة في التطبيقين
← العودة إلى Kotlin Multiplatform Academy