@State للكائنات المملوكة
امتلك نموذجًا قابلًا للمراقبة باستخدام @State
@State للكائنات المملوكة درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في SwiftUI Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Who Owns the Model
An observable model needs an owner: a view that creates it and keeps it alive for as long as the screen exists.
@State Owns the Instance
Use @State to let a view own an observable model. The view creates it once and holds onto it across redraws.
@State private var model = Counter()Why @State Here
Without @State, a fresh model would be made on every redraw, wiping your data. @State persists the same instance.
Reading the Model
Read tracked properties straight from the model in your body, and the view refreshes when they change.
Text("Count: \(model.count)")Calling Model Methods
Trigger behaviour by calling the model methods from your controls, keeping the view free of the actual logic.
Button("Add") { model.increment() }Mark It private
Keep owned state private so other types cannot reach in and mutate the model behind the view back.
@State private var model = Counter()No $ to Read
To read an observable model you use it directly. The dollar sign $ is only for making a binding, which we cover later.
A Full Owned View
Here a view owns the model, reads its value, and updates it on tap, all in one tidy place.
struct CounterView: View {
@State private var model = Counter()
var body: some View {
Button("\(model.count)") { model.increment() }
}
}Survives Redraws
Because @State is managed by SwiftUI, your model survives every body re-evaluation while the view is on screen.
One Owner Rule
A model should have a single owner. Child views receive access instead of each making their own copy.
Lifetime Tied to the View
The model lives as long as its owning view. When the view leaves the screen for good, the model is released.
Quick Check
Let us confirm how ownership works.
Recap
You used @State to let a view own its observable model, so a single instance survives redraws while the view reads and updates it. ✨
الأسئلة الشائعة
هل درس «@State للكائنات المملوكة» مجاني؟
نعم — نص درس «@State للكائنات المملوكة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
ماذا ستتعلم في «@State للكائنات المملوكة»؟
امتلك نموذجًا قابلًا للمراقبة باستخدام @State تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟
لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «@State للكائنات المملوكة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟
نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- من حالة الواجهة إلى فئة نموذج
- ماكرو @Observable
- @State للكائنات المملوكة
- مشاركة النماذج عبر @Environment