ရိုးရိုးလေး ရှင်းပြမယ်
Dictionaries ကို ရိုးရိုးလေးနားလည်မယ်
Dictionary က key-value pair နဲ့ data သိမ်းတဲ့ collection ပါ။ User profile, settings, country-capital mapping, product price table စတာတွေမှာအသုံးဝင်ပါတယ်။ Array က order အဓိကထားပေမယ့် Dictionary က key နဲ့ data ရှာတာကိုအဓိကထားပါတယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
capitals["Korea"]! လို့ force unwrap လုပ်ပြီး key မရှိရင် app crash ဖြစ်နိုင်ပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Dictionaries ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။
- Typed value
- Swift expression / function
- Safe result
Dictionaries ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
var capitals = [
"Myanmar": "Naypyidaw",
"Japan": "Tokyo"
]
if let myanmarCapital = capitals["Myanmar"] {
print("Capital of Myanmar is \(myanmarCapital)")
}
capitals["Thailand"] = "Bangkok"
print("Total countries: \(capitals.count)")Dictionaries ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Typed value
var capitals = [Dictionary က key-value pair နဲ့ data သိမ်းတဲ့ collection ပါ။ User profile, settings, country-capital mapping, product price table စတာတွေမှာအသုံးဝင်ပါတယ်။ Array က order အဓိကထားပေမယ့် Dictionary က key နဲ့ data ရှာတာကိုအဓိကထားပါတယ်။
- Swift expression / function
if let myanmarCapital = capitals["Myanmar"] {Dictionaries ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။
- Safe result
print("Total countries: \(capitals.count)")Dictionaries 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
Dictionaries ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
capitals["Korea"]! လို့ force unwrap လုပ်ပြီး key မရှိရင် app crash ဖြစ်နိုင်ပါတယ်။
Dictionary က key-value pair နဲ့ data သိမ်းတဲ့ collection ပါ။ User profile, settings, country-capital mapping, product price table စတာတွေမှာအသုံးဝင်ပါတယ်။ Array က order အဓိကထားပေမယ့် Dictionary က key နဲ့ data ရှာတာကိုအဓိကထားပါတယ်။
var capitals = [
"Myanmar": "Naypyidaw",
"Japan": "Tokyo"
]
if let myanmarCapital = capitals["Myanmar"] {
print("Capital of Myanmar is \(myanmarCapital)")
}
capitals["Thailand"] = "Bangkok"
print("Total countries: \(capitals.count)")Dictionary ထဲက value ကို key နဲ့ယူတဲ့အခါ result က optional ဖြစ်ပါတယ်။ အဲ့ဒီ key မရှိနိုင်လို့ပါ။ ဒါကြောင့် if let နဲ့ value ရှိမှ print လုပ်တာက safe ဖြစ်ပါတယ်။
Capital of Myanmar is Naypyidaw Total countries: 3Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Dictionaries example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Dictionaries 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 ကိုခန့်မှန်းပါ
var capitals = [
"Myanmar": "Naypyidaw",
"Japan": "Tokyo"
]
if let myanmarCapital = capitals["Myanmar"] {
print("Capital of Myanmar is \(myanmarCapital)")
}
capitals["Thailand"] = "Bangkok"
print("Total countries: \(capitals.count)")ရလဒ်: Dictionaries ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Dictionaries 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
- Dictionaries ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။
- capitals["Korea"]! လို့ force unwrap လုပ်ပြီး key မရှိရင် app crash ဖြစ်နိုင်ပါတယ်။
- Dictionaries 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