0Pricing
SwiftUI Academy · درس

Identifiable والمعرّفات المستقرة

اجعل النماذج من نوع Identifiable لإجراء المقارنة التفاضلية بشكل صحيح

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

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

The Diffing Problem

When data changes, SwiftUI must know which rows moved, stayed, or vanished. Stable identity makes that diffing correct. 🧩

Why id: .self Is Fragile

Using id: .self breaks when two items are equal or a value changes, because the identity shifts unexpectedly.

Meet Identifiable

The Identifiable protocol asks a type for just one thing: a stable, unique id property that never changes for that item.

protocol Identifiable {
    var id: ID { get }
}

Conforming a Model

Add an id property and conform to Identifiable, and your struct is ready for clean, reliable lists.

struct Task: Identifiable {
    let id = UUID()
    var title: String
}

UUID for Fresh IDs

A UUID generates a guaranteed-unique value, perfect when items have no natural identifier of their own.

let id = UUID()

ForEach Without id:

Once a type is Identifiable, ForEach finds the id by itself, so you can drop the id parameter entirely.

ForEach(tasks) { task in
    Text(task.title)
}

Stable Means Persistent

A good id never changes for the same logical item, even when its title or other fields are edited later on.

Natural Keys

If your data already has a unique field, like a database id, use it instead of generating a new one.

struct User: Identifiable {
    let id: Int
    var name: String
}

Why It Matters for Animation

Correct identity lets SwiftUI animate moves and deletes smoothly instead of redrawing everything.

Avoid Index as ID

Using array indexes as ids is risky: inserting or deleting shifts every index and confuses diffing.

Identity Is About Sameness

Two items with the same id are treated as the same row; different ids mean different rows. Choose ids carefully.

Quick Check

Pick the most reliable approach to row identity.

Recap

You learned Identifiable: a stable id per item lets SwiftUI diff and animate lists correctly, far safer than id: .self. 🎉

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

هل درس «Identifiable والمعرّفات المستقرة» مجاني؟

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

ماذا ستتعلم في «Identifiable والمعرّفات المستقرة»؟

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

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

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

كم من الوقت يستغرق درس «Identifiable والمعرّفات المستقرة»؟

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

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

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

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

  1. إنشاء قائمة ثابتة
  2. التكرار باستخدام ForEach
  3. Identifiable والمعرّفات المستقرة
  4. الأقسام وأنماط القوائم
← العودة إلى SwiftUI Academy