Dictionary က key-value pair နဲ့ data သိမ်းတဲ့ collection ပါ။ User profile, settings, country-capital mapping, product price table စတာတွေမှာအသုံးဝင်ပါတယ်။ Array က order အဓိကထားပေမယ့် Dictionary က key နဲ့ data ရှာတာကိုအဓိကထားပါတယ်။
swift
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 ဖြစ်ပါတယ်။
You should see
Capital of Myanmar is Naypyidaw Total countries: 3