Thuta Learning
ရှာဖွေရန်
ExercisesMobile Developmentintermediate

လက်တွေ့ လေ့ကျင့်ခန်း အစုံ ၁

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • လက်တွေ့ လေ့ကျင့်ခန်း အစုံ ၁ ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • ကိုယ်တိုင် Xcode/SwiftUI code ကို run ကြည့်တတ်မယ်
  • Real project ထဲမှာ ဒီ concept ကို ချက်ချင်း အသုံးချတတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

ဒီ round က learn ခဲ့ပြီးသား Basic/Intermediate concept တွေကို လက်တွေ့ ပြန်စမ်းကြည့်ဖို့ round ပါ — View/@State/Modifier ကို ခွဲသိခြင်း, SwiftUI code ထဲက error ကို ရှာဖွေခြင်း, `@State` ကို မှန်ကန်စွာ သုံးတတ်ခြင်းကိုပဲ ကိုယ်တိုင် လက်တွေ့ လုပ်ကြည့်ရမှာပါ။ Task တစ်ခုချင်းစီက ၅ မိနစ်ထက် မကြာသင့်ပါဘူး။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Task 1: 'View protocol', '@State', 'Modifier' သုံးလုံးရဲ့ တာဝန် ကွာခြားချက်ကို ဝါကျတစ်ကြောင်းစီနဲ့ ရှင်းပြပါ။ Task 2: `var count = 0` (`@State` မပါ) ကို View struct ထဲမှာ ရေးပြီး Button tap ရင် `count += 1` လုပ်ထားရင် ဘာဖြစ်မလဲ ခန့်မှန်းပြီး ရေးပါ။ Task 3: `List` ကို ဘယ်အခါ VStack+ForEach အစား သုံးသင့်လဲ ဥပမာနဲ့တကွ ရေးပါ။ Task 4: `@State` နဲ့ `@Binding` ရဲ့ ကွာခြားချက်ကို parent-child View ဆက်သွယ်ပုံ ဥပမာနဲ့ ရှင်းပါ။

အတူတူ ကြည့်မယ်

text
# Task 2 - mutable property without @State
var count = 0   // NOT wrapped in @State!

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

Result: this is a compile-time error — SwiftUI View is a
struct (immutable), so a plain `var` property cannot be
mutated from inside a closure. The compiler says
"Cannot assign to property: 'self' is immutable".
@State is required to make it mutable.

# Task 4 - @State vs @Binding
@State  = the View that OWNS the value (private, source of truth)
@Binding = a child View that needs to READ AND WRITE a value
           it doesn't own — the parent passes it in with $
You should see
View/@State/Modifier ကွာခြားချက်၊ @State-less compile error၊ @State vs @Binding ဆုံးဖြတ်ချက် ရရှိလာမည်။

၅ မိနစ် စမ်းကြည့်

ကိုယ်ပိုင် View တစ်ခု ရေးပြီး တမင် `@State` မထည့်ဘဲ mutable property bug ထည့်ကြည့်ပါ — Xcode ရဲ့ compile error message ကို ကိုယ်တိုင် ဖတ်ကြည့်ပါ။

သတိလေးတစ်ချက်

ဒီ round မှာ physical device setup လုပ်ဖို့ မလိုပါဘူး — Simulator (ဒါမှမဟုတ် #Preview panel) ကိုပဲ အဓိကထား practice လုပ်ခြင်းပါ။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • View/@State/Modifier ကို 'အားလုံးတူတူပဲ' လို့ ရှုပ်ထွေးနေခြင်း
  • SwiftUI compile error message ကို ဖတ်ရှုချသွားပြီး error ဘယ် line မှာလဲ မကြည့်ခြင်း

အခု ကိုယ်တိုင် စမ်းကြည့်

ကိုယ်ပိုင် View တစ်ခု ရေးပြီး တမင် `@State` မထည့်ဘဲ mutable property bug ထည့်ကြည့်ပါ — Xcode ရဲ့ compile error message ကို ကိုယ်တိုင် ဖတ်ကြည့်ပါ။

You'll know it worked when: View/@State/Modifier ကွာခြားချက်၊ @State-less compile error၊ @State vs @Binding ဆုံးဖြတ်ချက် ရရှိလာမည်။

လက်တွေ့ လေ့ကျင့်ခန်း အစုံ ၁ | Thuta Learning