0Pricing
SwiftUI Academy · درس

@State مقابل @Binding

اختر الملكية المناسبة لكل خاصية

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

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

Two Wrappers, One Goal

Both @State and @Binding manage mutable data, but they play different roles. Picking the right one keeps ownership clear. 🎯

@State Owns

Use @State when this view is the source of truth. It creates and stores the value for itself.

@State private var count = 0

@Binding Borrows

Use @Binding when the value lives somewhere else and this view only needs to read and write it.

@Binding var count: Int

Who Creates the Value

@State initializes a value. @Binding never does; it always receives one from a parent that owns it.

Mark State Private

@State is view-local, so mark it private. No one outside should reach in and set it directly.

@State private var isOn = false

Bindings Stay Open

A @Binding is not private; the parent must be able to pass a value in when it creates the child.

The Dollar Sign Link

An owners @State turns into a child @Binding through the dollar sign. That is the bridge between the two.

ChildView(count: $count)

Top of the Tree

The view highest up that truly owns the data uses @State. Everything below it borrows with @Binding.

A Simple Rule

Ask: do I create this value? If yes, @State. If a parent hands it to me, @Binding. That choice rarely fails you.

Wrong Choice, Real Bugs

Use @State where you meant @Binding and the child edits a copy. The parent never sees the change, and the UI quietly desyncs.

Same Value, Two Roles

One piece of data is @State in its owner and @Binding in every borrower. Same value, different responsibilities. 🔄

Quick Check

Quick check on choosing wrappers.

Recap: Own or Borrow

Use @State to own a value and @Binding to borrow one. Match the wrapper to who creates the data and your views stay in sync. 🧠

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

هل درس «@State مقابل @Binding» مجاني؟

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

ماذا ستتعلم في «@State مقابل @Binding»؟

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

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

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

كم من الوقت يستغرق درس «@State مقابل @Binding»؟

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

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

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

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

  1. لماذا تحتاج العناصر الفرعية إلى @Binding
  2. تمرير Binding إلى الأسفل
  3. بناء صف Toggle قابل لإعادة الاستخدام
  4. @State مقابل @Binding
← العودة إلى SwiftUI Academy