ရိုးရိုးလေး ရှင်းပြမယ်
Switch Statements ကို ရိုးရိုးလေးနားလည်မယ်
switch က value တစ်ခုကို case များစွာနဲ့တိတိကျကျစစ်ဖို့အသုံးဝင်ပါတယ်။ Menu selection, app route, user status, payment status, error type စတာတွေကိုဖတ်ရလွယ်အောင်စီစဉ်နိုင်ပါတယ်။ Swift switch က range, multiple cases, pattern matching, where condition တွေကိုပါ support လုပ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Possible case များကိုမစဉ်းစားဘဲ default ထဲမှာပဲအကုန်ပစ်ထည့်တာက debugging ခက်စေနိုင်ပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Switch Statements မှာinput domain, exhaustive paths, return value, side effects နဲ့failure/cancellation behavior ကိုရှင်းပါ။ Force unwrap နဲ့silent error suppression မသုံးဘဲguard, typed errors နဲ့explicit propagation ကိုဦးစားပေးပါ။
- Typed value
- Swift expression / function
- Safe result
Switch Statements ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
let paymentStatus = "pending"
switch paymentStatus {
case "paid":
print("Access granted")
case "pending":
print("Payment is still processing")
case "failed", "cancelled":
print("Please try payment again")
default:
print("Unknown payment status")
}Switch Statements မှာinput domain, exhaustive paths, return value, side effects နဲ့failure/cancellation behavior ကိုရှင်းပါ။ Force unwrap နဲ့silent error suppression မသုံးဘဲguard, typed errors နဲ့explicit propagation ကိုဦးစားပေးပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Typed value
let paymentStatus = "pending"switch က value တစ်ခုကို case များစွာနဲ့တိတိကျကျစစ်ဖို့အသုံးဝင်ပါတယ်။ Menu selection, app route, user status, payment status, error type စတာတွေကိုဖတ်ရလွယ်အောင်စီစဉ်နိုင်ပါတယ်။ Swift switch က range, multiple cases, pattern matching, where condition တွေကိုပါ support လုပ်ပါတယ်။
- Swift expression / function
print("Payment is still processing")Switch Statements မှာinput domain, exhaustive paths, return value, side effects နဲ့failure/cancellation behavior ကိုရှင်းပါ။ Force unwrap နဲ့silent error suppression မသုံးဘဲguard, typed errors နဲ့explicit propagation ကိုဦးစားပေးပါ။
- Safe result
}Switch Statements example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
- Typed value
- Swift expression / function
- Safe result
Success
Switch Statements ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
Possible case များကိုမစဉ်းစားဘဲ default ထဲမှာပဲအကုန်ပစ်ထည့်တာက debugging ခက်စေနိုင်ပါတယ်။
switch က value တစ်ခုကို case များစွာနဲ့တိတိကျကျစစ်ဖို့အသုံးဝင်ပါတယ်။ Menu selection, app route, user status, payment status, error type စတာတွေကိုဖတ်ရလွယ်အောင်စီစဉ်နိုင်ပါတယ်။ Swift switch က range, multiple cases, pattern matching, where condition တွေကိုပါ support လုပ်ပါတယ်။
let paymentStatus = "pending"
switch paymentStatus {
case "paid":
print("Access granted")
case "pending":
print("Payment is still processing")
case "failed", "cancelled":
print("Please try payment again")
default:
print("Unknown payment status")
}paymentStatus တန်ဖိုးကို case တစ်ခုချင်းစစ်ပါတယ်။ pending နဲ့ကိုက်တဲ့အတွက် processing message ထုတ်ပါတယ်။ Swift switch မှာ case အားလုံး cover မဖြစ်ရင် default ထည့်ပေးရပါတယ်။
Payment is still processingReal-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Switch Statements example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Switch Statements example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
let paymentStatus = "pending"
switch paymentStatus {
case "paid":
print("Access granted")
case "pending":
print("Payment is still processing")
case "failed", "cancelled":
print("Please try payment again")
default:
print("Unknown payment status")
}ရလဒ်: Switch Statements ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Switch Statements example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Typed value → Swift expression / function → Safe result
- Switch Statements မှာinput domain, exhaustive paths, return value, side effects နဲ့failure/cancellation behavior ကိုရှင်းပါ။ Force unwrap နဲ့silent error suppression မသုံးဘဲguard, typed errors နဲ့explicit propagation ကိုဦးစားပေးပါ။
- Possible case များကိုမစဉ်းစားဘဲ default ထဲမှာပဲအကုန်ပစ်ထည့်တာက debugging ခက်စေနိုင်ပါတယ်။
- Switch Statements example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
The Swift Programming Language — Swift.org