Thuta Learning
Swift
IntermediateProgrammingအခြေခံ

Optionals

Swiftသင်ခန်းစာ 1520 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Optionals ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
  • Nil, error, boundary နဲ့concurrency cases စမ်းနိုင်ရန်
  • Safe, testable Swift code ရေးနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

Optionals ကို ရိုးရိုးလေးနားလည်မယ်

Optional က Swift ရဲ့အရေးကြီးဆုံး concept တွေထဲကတစ်ခုပါ။ Optional ဆိုတာ တန်ဖိုးရှိနိုင်တယ်၊ မရှိလည်းဖြစ်နိုင်တယ်ဆိုတဲ့ data ကို safe ဖြစ်အောင်ကိုင်တွယ်တဲ့နည်းလမ်းပါ။ တန်ဖိုးမရှိတာကို nil လို့ခေါ်ပါတယ်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

displayName! ကို value မရှိတဲ့အချိန် force unwrap လုပ်ရင် runtime crash ဖြစ်နိုင်ပါတယ်။

ဖြစ်လေ့ရှိတဲ့အမှား

Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

Optionals မှာinput domain, exhaustive paths, return value, side effects နဲ့failure/cancellation behavior ကိုရှင်းပါ။ Force unwrap နဲ့silent error suppression မသုံးဘဲguard, typed errors နဲ့explicit propagation ကိုဦးစားပေးပါ။

Mental Model
  1. Optional / fallible input
  2. Pattern + error handling
  3. Safe value / recovery

Optionals ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

swift
var displayName: String? = "Sai Tun"

if let name = displayName {
    print("Hello, \(name)")
} else {
    print("Hello, guest")
}

let fallbackName = displayName ?? "Guest"
print("Profile: \(fallbackName)")

Optionals မှာinput domain, exhaustive paths, return value, side effects နဲ့failure/cancellation behavior ကိုရှင်းပါ။ Force unwrap နဲ့silent error suppression မသုံးဘဲguard, typed errors နဲ့explicit propagation ကိုဦးစားပေးပါ။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. Optional / fallible inputvar displayName: String? = "Sai Tun"

    Optional က Swift ရဲ့အရေးကြီးဆုံး concept တွေထဲကတစ်ခုပါ။ Optional ဆိုတာ တန်ဖိုးရှိနိုင်တယ်၊ မရှိလည်းဖြစ်နိုင်တယ်ဆိုတဲ့ data ကို safe ဖြစ်အောင်ကိုင်တွယ်တဲ့နည်းလမ်းပါ။ တန်ဖိုးမရှိတာကို nil လို့ခေါ်ပါတယ်။

  2. Pattern + error handlingprint("Hello, guest")

    Optionals မှာinput domain, exhaustive paths, return value, side effects နဲ့failure/cancellation behavior ကိုရှင်းပါ။ Force unwrap နဲ့silent error suppression မသုံးဘဲguard, typed errors နဲ့explicit propagation ကိုဦးစားပေးပါ။

  3. Safe value / recoveryprint("Profile: \(fallbackName)")

    Optionals example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

Execution Flow
  1. Optional / fallible input
  2. Pattern + error handling
  3. Safe value / recovery

Success

Optionals ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

Failure

displayName! ကို value မရှိတဲ့အချိန် force unwrap လုပ်ရင် runtime crash ဖြစ်နိုင်ပါတယ်။

Optional က Swift ရဲ့အရေးကြီးဆုံး concept တွေထဲကတစ်ခုပါ။ Optional ဆိုတာ တန်ဖိုးရှိနိုင်တယ်၊ မရှိလည်းဖြစ်နိုင်တယ်ဆိုတဲ့ data ကို safe ဖြစ်အောင်ကိုင်တွယ်တဲ့နည်းလမ်းပါ။ တန်ဖိုးမရှိတာကို nil လို့ခေါ်ပါတယ်။

API response, form input, dictionary lookup, user profile image စတာတွေမှာ data မရှိနိုင်တဲ့ case တွေအများကြီးရှိပါတယ်။ Optional က အဲဒီ case တွေကို crash မဖြစ်အောင် စနစ်တကျကိုင်တွယ်ပေးပါတယ်။

swift
var displayName: String? = "Sai Tun"

if let name = displayName {
    print("Hello, \(name)")
} else {
    print("Hello, guest")
}

let fallbackName = displayName ?? "Guest"
print("Profile: \(fallbackName)")

String? က string တန်ဖိုးရှိနိုင်သလို nil လည်းဖြစ်နိုင်တယ်ဆိုတာပါ။ if let က optional ထဲမှာ value ရှိမှ name အနေနဲ့ယူသုံးပါတယ်။ ?? က nil ဖြစ်ရင် fallback value သုံးဖို့ပါ။

You should see
Hello, Sai Tun Profile: Sai Tun

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Optionals example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Optionals 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 ကိုခန့်မှန်းပါ
swift
var displayName: String? = "Sai Tun"

if let name = displayName {
    print("Hello, \(name)")
} else {
    print("Hello, guest")
}

let fallbackName = displayName ?? "Guest"
print("Profile: \(fallbackName)")

ရလဒ်: Optionals ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

အရိပ်အမြွက် ကြည့်ရန်

Optionals example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

Quick Quiz 1 / 3

Optionals ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • Optional / fallible input → Pattern + error handling → Safe value / recovery
  • Optionals မှာinput domain, exhaustive paths, return value, side effects နဲ့failure/cancellation behavior ကိုရှင်းပါ။ Force unwrap နဲ့silent error suppression မသုံးဘဲguard, typed errors နဲ့explicit propagation ကိုဦးစားပေးပါ။
  • displayName! ကို value မရှိတဲ့အချိန် force unwrap လုပ်ရင် runtime crash ဖြစ်နိုင်ပါတယ်။
  • Optionals 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

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

  • displayName! ကို value မရှိတဲ့အချိန် force unwrap လုပ်ရင် runtime crash ဖြစ်နိုင်ပါတယ်။
  • Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
  • Force unwrap သို့unchecked shared mutable state ဖြင့်compiler safety ကိုကျော်ခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

Optionals example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Hello, Sai Tun Profile: Sai Tun

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Optionals | Thuta Learning