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

SwiftUI Basics

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

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

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

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

အရင်တုန်းက iOS UI ကို UIKit (imperative, 'ဒီ view ကို ဒီလို update လုပ်ပါ' ဆိုတဲ့ ပုံစံ) ဒါမှမဟုတ် Storyboard (visual drag-and-drop) နဲ့ ရေးရပါတယ်။ SwiftUI ကတော့ declarative UI framework ပါ — `View` protocol ကို conform လုပ်ထားတဲ့ struct ထဲမှာ 'UI ဘယ်လို ပုံစံ ဖြစ်စေချင်တယ်' ဆိုတာကို Swift code နဲ့ တိုက်ရိုက် ရေးပါတယ်, data ပြောင်းလဲတိုင်း SwiftUI က UI ကို automatic re-render ပေးပါတယ် — Android ရဲ့ Jetpack Compose concept နဲ့ တူညီပါတယ် (ဒီ site ရဲ့ Android tutorial ဖတ်ဖူးရင် ချက်ချင်း ရင်းနှီးမှာပါ).

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

`struct Greeting: View { let name: String; var body: some View { Text("Hello, \(name)!") } }` လို့ ရေးလိုက်ရင်, `Greeting(name: "Aye Aye")` ကို ခေါ်တာနဲ့ 'Hello, Aye Aye!' ဆိုတဲ့ text ကို screen ပေါ် ပြပေးပါတယ်။ Xcode ရဲ့ `#Preview` macro ကို ထည့်ရင် Simulator run စရာ မလိုဘဲ UI ကို ချက်ချင်း ကြည့်လို့ရပါတယ်.

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

swift
import SwiftUI

struct Greeting: View {
    let name: String

    var body: some View {
        Text("Hello, \(name)!")
    }
}

#Preview {
    Greeting(name: "Aye Aye")
}
You should see
Xcode Preview panel ထဲမှာ 'Hello, Aye Aye!' ဆိုတဲ့ text ကို Simulator run စရာမလိုဘဲ ချက်ချင်း တွေ့ရမည်။

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

`Greeting` View ကို ကိုယ်တိုင်ရေးပြီး `#Preview` panel ထဲ ကြည့်ကြည့်ပါ — name parameter ကို ပြောင်းကြည့်ပြီး preview auto-update ဖြစ်တာ confirm လုပ်ကြည့်ပါ။

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

`#Preview` က UI layout ကို quick ကြည့်ဖို့ပါ — button tap, navigation စတဲ့ interactive behavior ကို Simulator (ဒါမှမဟုတ် physical device) ပေါ်မှာသာ တကယ်တမ်း test လုပ်နိုင်ပါတယ်။

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

  • `var body: some View { ... }` ကို ချန်ထားခြင်း — View protocol requirement ကို မဖြည့်ဆည်းလို့ compile error ဖြစ်ပါလိမ့်မယ်
  • SwiftUI View ကို UIKit-era imperative style (view reference ကို manual update) နဲ့ ရေးဖို့ ကြိုးစားခြင်း — SwiftUI ရဲ့ declarative pattern ကို လိုက်နာသင့်ပါတယ်

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

`Greeting` View ကို ကိုယ်တိုင်ရေးပြီး `#Preview` panel ထဲ ကြည့်ကြည့်ပါ — name parameter ကို ပြောင်းကြည့်ပြီး preview auto-update ဖြစ်တာ confirm လုပ်ကြည့်ပါ။

You'll know it worked when: Xcode Preview panel ထဲမှာ 'Hello, Aye Aye!' ဆိုတဲ့ text ကို Simulator run စရာမလိုဘဲ ချက်ချင်း တွေ့ရမည်။

SwiftUI Basics | Thuta Learning