من حالة الواجهة إلى فئة نموذج
استخرج البيانات المشتركة إلى فئة
من حالة الواجهة إلى فئة نموذج درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في SwiftUI Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Outgrowing @State
When a screen has many @State values, your view starts juggling data and UI at once. That mix gets messy fast as the screen grows. 🤔
What a Model Holds
A model is a plain object that holds your data and the rules around it, kept separate from how things look on screen.
A Simple Model Class
Start by moving related values into one class so they live together in a single, tidy place.
class Counter {
var count = 0
}Methods Carry the Logic
Put behaviour next to the data with methods, so the view just asks the model to act instead of doing the work itself.
class Counter {
var count = 0
func increment() { count += 1 }
}Why a Class, Not a Struct
A class is a reference type, so every view sharing the same instance sees the exact same data, not its own private copy.
Views Stay Thin
With logic in the model, your view body shrinks to mostly layout. A thin view is far easier to read and change later.
One Source of Truth
Keeping data in the model gives you one source of truth, so screens never disagree about the current value.
Reusing the Model
Because the model is independent of any view, you can reuse it across several screens or even in tests.
Holding It in a View
A view can create and hold a model instance as a property, ready to read its values and call its methods.
struct CounterView: View {
let model = Counter()
var body: some View { Text("Count") }
}The Missing Piece
So far the view can read the model, but it will not yet refresh when the data changes. We will fix that with observation next.
Separation of Concerns
This split is called separation of concerns: the view shows, the model decides. Each part has one clear job.
Quick Check
You moved your data into a class. Quick question about why.
Recap
You learned to lift data and logic out of a view into a model class, keeping views thin and giving your app one source of truth. ✨
الأسئلة الشائعة
هل درس «من حالة الواجهة إلى فئة نموذج» مجاني؟
نعم — نص درس «من حالة الواجهة إلى فئة نموذج» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
ماذا ستتعلم في «من حالة الواجهة إلى فئة نموذج»؟
استخرج البيانات المشتركة إلى فئة تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟
لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «من حالة الواجهة إلى فئة نموذج»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟
نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- من حالة الواجهة إلى فئة نموذج
- ماكرو @Observable
- @State للكائنات المملوكة
- مشاركة النماذج عبر @Environment