0Pricing
SwiftUI Academy · درس

إعلان خصائص @State

أضف @State للاحتفاظ ببيانات قابلة للتغيير محلية لواجهة View

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

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

Meet the @State Wrapper

You declare mutable view-local data by marking a property with @State. This property wrapper tells SwiftUI to store and watch the value for you. 🏷️

@State private var count = 0

Always Give It a Default

A @State property needs an initial value right where you declare it. SwiftUI uses it to create the storage the first time the view appears.

@State private var isOn = false

Mark It private

State belongs to one view, so declare it private. This signals that no outside code should reach in and set it directly, keeping ownership clear.

@State private var name = ""

Reading the Value

Read a @State property like any normal variable. Just use its name in body and SwiftUI tracks that this view depends on it.

Text("You tapped \(count) times")

Writing the Value

You can mutate a @State property even though the view is a struct. Assign to it from an action and SwiftUI schedules a redraw automatically.

Button("Add") { count += 1 }

It Persists Across Redraws

SwiftUI keeps the @State value alive across the many times the view body is recomputed. Your count won't reset just because the view redrew.

Putting It Together

Declare state, read it in the body, and mutate it from a button. That is the full loop that powers most simple interactions.

struct CounterView: View {
    @State private var count = 0
    var body: some View {
        Button("Count \(count)") { count += 1 }
    }
}

Use Simple Value Types

@State works best with value types like Int, Bool, String, or small structs. For shared objects you'll later reach for other tools, but values are the norm here.

One Owner Per Property

The view that declares @State is its owner. Other views can receive access, but only one view should hold the original state for a given value.

Don't Overuse It

Reach for @State only when a value truly changes while the view is on screen. Constant labels and passed-in data don't need it.

Naming for Clarity

Name state for what it represents, like isLoading or selectedTab. Clear names make the data flow obvious to anyone reading the view.

Quick Check

Which line correctly declares a private piece of view-local state?

Recap

Declare changing view data with @State, give it a default, and keep it private. Read it freely in body and mutate it from actions to trigger redraws. ✅

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

هل درس «إعلان خصائص @State» مجاني؟

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

ماذا ستتعلم في «إعلان خصائص @State»؟

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

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

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

كم من الوقت يستغرق درس «إعلان خصائص @State»؟

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

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

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

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

  1. لماذا تحتاج View إلى الحالة
  2. إعلان خصائص @State
  3. إنشاء عدّاد للنقرات
  4. تبديل الظهور باستخدام الحالة
← العودة إلى SwiftUI Academy