ရိုးရိုးလေး ရှင်းပြမယ်
Enumerations ကို ရိုးရိုးလေးနားလည်မယ်
Enum က ဆက်စပ်နေတဲ့ possible values တွေကို တစ်စုတည်းသတ်မှတ်ဖို့သုံးပါတယ်။ App status, screen route, payment state, direction, theme mode စတာတွေကို string random ရေးတာထက် enum နဲ့ရေးတာပိုလုံခြုံပါတယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
String status တွေကိုနေရာတိုင်း hardcode လုပ်တာထက် enum သတ်မှတ်တာက maintain လုပ်ရပိုလွယ်ပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Enumerations ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- Optional / fallible input
- Pattern + error handling
- Safe value / recovery
Enumerations ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
enum AppTheme {
case light
case dark
case system
}
let selectedTheme = AppTheme.dark
switch selectedTheme {
case .light:
print("Use light colors")
case .dark:
print("Use dark colors")
case .system:
print("Follow device setting")
}Enumerations ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Optional / fallible input
enum AppTheme {Enum က ဆက်စပ်နေတဲ့ possible values တွေကို တစ်စုတည်းသတ်မှတ်ဖို့သုံးပါတယ်။ App status, screen route, payment state, direction, theme mode စတာတွေကို string random ရေးတာထက် enum နဲ့ရေးတာပိုလုံခြုံပါတယ်။
- Pattern + error handling
case .light:Enumerations ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- Safe value / recovery
}Enumerations example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
- Optional / fallible input
- Pattern + error handling
- Safe value / recovery
Success
Enumerations ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
String status တွေကိုနေရာတိုင်း hardcode လုပ်တာထက် enum သတ်မှတ်တာက maintain လုပ်ရပိုလွယ်ပါတယ်။
Enum က ဆက်စပ်နေတဲ့ possible values တွေကို တစ်စုတည်းသတ်မှတ်ဖို့သုံးပါတယ်။ App status, screen route, payment state, direction, theme mode စတာတွေကို string random ရေးတာထက် enum နဲ့ရေးတာပိုလုံခြုံပါတယ်။
enum AppTheme {
case light
case dark
case system
}
let selectedTheme = AppTheme.dark
switch selectedTheme {
case .light:
print("Use light colors")
case .dark:
print("Use dark colors")
case .system:
print("Follow device setting")
}AppTheme enum မှာ app theme ဖြစ်နိုင်တဲ့ option သုံးခုကိုသတ်မှတ်ထားပါတယ်။ switch နဲ့ enum case အားလုံးကို handle လုပ်ထားတာကြောင့် future bug တွေလျော့ပါတယ်။
Use dark colorsReal-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Enumerations example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Enumerations 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 ကိုခန့်မှန်းပါ
enum AppTheme {
case light
case dark
case system
}
let selectedTheme = AppTheme.dark
switch selectedTheme {
case .light:
print("Use light colors")
case .dark:
print("Use dark colors")
case .system:
print("Follow device setting")
}ရလဒ်: Enumerations ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Enumerations 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
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Optional / fallible input → Pattern + error handling → Safe value / recovery
- Enumerations ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- String status တွေကိုနေရာတိုင်း hardcode လုပ်တာထက် enum သတ်မှတ်တာက maintain လုပ်ရပိုလွယ်ပါတယ်။
- Enumerations 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