Thuta Learning
ရှာဖွေရန်
BasicProgrammingbeginner

Dictionaries

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

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

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

  • capitals["Korea"]! လို့ force unwrap လုပ်ပြီး key မရှိရင် app crash ဖြစ်နိုင်ပါတယ်။
Dictionaries | Thuta Learning