Thuta Learning
Swift
ExercisesProgrammingအခြေခံ

Exercise Set 2: Struct, Protocol & Error Handling Practice

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

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

  • Exercise Set 2: Struct, Protocol & Error Handling Practice ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
  • Nil, error, boundary နဲ့concurrency cases စမ်းနိုင်ရန်
  • Safe, testable Swift code ရေးနိုင်ရန်

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

Exercise Set 2: Struct, Protocol & Error Handling Practice ကို ရိုးရိုးလေးနားလည်မယ်

ဒီ exercise set က Exercise Set 1 ထက် difficulty level မြင့်ပြီး Intermediate/Advanced chapter မှာ သင်ခဲ့တဲ့ protocol, closure, enum with associated value, error handling topic တွေကို ပေါင်းစပ် practice လုပ်ဖို့ ရည်ရွယ်ထားပါတယ်။ Teaching content အသစ် မဟုတ်ဘဲ concept တွေကို ကိုယ်တိုင် code ရေးပြီး apply လုပ်တဲ့ self-test ဖြစ်ပါတယ်။ Task တွေက Task Manager project ထက် independent ဖြစ်ပြီး concept တစ်ခုချင်းစီကို isolated အနေနဲ့ practice လုပ်နိုင်အောင် ဒီဇိုင်းလုပ်ထားပါတယ်။ ဖြေပြီးတိုင်း compile error တွေကို ကိုယ်တိုင် ဖတ်ပြီး fix ကြည့်ပါ။

Problem → Why → Solution

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

protocol requirement ကို { get } declare ထားပေမယ့် implement လုပ်တဲ့အခါ stored property အနေနဲ့ ထားပြီး computed property logic ကို လုံးဝ မထည့်တတ်ကြတယ်

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

throws function ကနေ error catch လုပ်တဲ့အခါ switch-style pattern matching (catch InvalidAgeError.outOfRange) ကို မသုံးဘဲ general catch { } တစ်ခုတည်းနဲ့ error detail ဆုံးရှုံးတတ်ကြတယ်

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

Exercise Set 2: Struct, Protocol & Error Handling Practice ကိုUI, domain state, persistence/service နဲ့tests ခွဲထားသောvertical slices ဖြင့်လုပ်ပါ။ Normal, boundary, nil, error, cancellation နဲ့concurrency paths ကိုrepeatable tests ဖြင့်စစ်ပြီးmain-actor work ကိုမပိတ်ပါနှင့်။

Mental Model
  1. လိုအပ်ချက်
  2. စဉ်းစားပြီး implement
  3. စမ်းသပ်ပြီးရှင်းပြ

Exercise Set 2: Struct, Protocol & Error Handling Practice ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

swift
// Task 1
protocol Shape {
    var area: Double { get }
}

struct Circle: Shape {
    let radius: Double
    var area: Double {
        // TODO: implement
        return 0
    }
}

struct Rectangle: Shape {
    let width: Double
    let height: Double
    var area: Double {
        // TODO: implement
        return 0
    }
}

// Task 2
let circles = [Circle(radius: 3), Circle(radius: 1), Circle(radius: 2)]
// TODO: sorted(by:) closure

// Task 3
enum NetworkResult {
    case success(data: String)
    case failure(reason: String)
}
// TODO: switch statement

// Task 4
enum InvalidAgeError: Error {
    case outOfRange
}
func validateAge(_ age: Int) throws -> Int {
    // TODO: implement
    return age
}

Exercise Set 2: Struct, Protocol & Error Handling Practice ကိုUI, domain state, persistence/service နဲ့tests ခွဲထားသောvertical slices ဖြင့်လုပ်ပါ။ Normal, boundary, nil, error, cancellation နဲ့concurrency paths ကိုrepeatable tests ဖြင့်စစ်ပြီးmain-actor work ကိုမပိတ်ပါနှင့်။

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

  1. လိုအပ်ချက်// Task 1

    ဒီ exercise set က Exercise Set 1 ထက် difficulty level မြင့်ပြီး Intermediate/Advanced chapter မှာ သင်ခဲ့တဲ့ protocol, closure, enum with associated value, error handling topic တွေကို ပေါင်းစပ် practice လုပ်ဖို့ ရည်ရွယ်ထားပါတယ်။ Teaching content အသစ် မဟုတ်ဘဲ concept တွေကို ကိုယ်တိုင် code ရေးပြီး apply လုပ်တဲ့ self-test ဖြစ်ပါတယ်။ Task တွေက Task Manager project ထက် independent ဖြစ်ပြီး concept တစ်ခုချင်းစီကို isolated အနေနဲ့ practice လုပ်နိုင်အောင် ဒီဇိုင်းလုပ်ထားပါတယ်။ ဖြေပြီးတိုင်း compile error တွေကို ကိုယ်တိုင် ဖတ်ပြီး fix ကြည့်ပါ။

  2. စဉ်းစားပြီး implement}

    Exercise Set 2: Struct, Protocol & Error Handling Practice ကိုUI, domain state, persistence/service နဲ့tests ခွဲထားသောvertical slices ဖြင့်လုပ်ပါ။ Normal, boundary, nil, error, cancellation နဲ့concurrency paths ကိုrepeatable tests ဖြင့်စစ်ပြီးmain-actor work ကိုမပိတ်ပါနှင့်။

  3. စမ်းသပ်ပြီးရှင်းပြ}

    Exercise Set 2: Struct, Protocol & Error Handling Practice example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

Execution Flow
  1. လိုအပ်ချက်
  2. စဉ်းစားပြီး implement
  3. စမ်းသပ်ပြီးရှင်းပြ

Success

Exercise Set 2: Struct, Protocol & Error Handling Practice ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

Failure

protocol requirement ကို { get } declare ထားပေမယ့် implement လုပ်တဲ့အခါ stored property အနေနဲ့ ထားပြီး computed property logic ကို လုံးဝ မထည့်တတ်ကြတယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

ဒီ exercise set က Exercise Set 1 ထက် difficulty level မြင့်ပြီး Intermediate/Advanced chapter မှာ သင်ခဲ့တဲ့ protocol, closure, enum with associated value, error handling topic တွေကို ပေါင်းစပ် practice လုပ်ဖို့ ရည်ရွယ်ထားပါတယ်။ Teaching content အသစ် မဟုတ်ဘဲ concept တွေကို ကိုယ်တိုင် code ရေးပြီး apply လုပ်တဲ့ self-test ဖြစ်ပါတယ်။ Task တွေက Task Manager project ထက် independent ဖြစ်ပြီး concept တစ်ခုချင်းစီကို isolated အနေနဲ့ practice လုပ်နိုင်အောင် ဒီဇိုင်းလုပ်ထားပါတယ်။ ဖြေပြီးတိုင်း compile error တွေကို ကိုယ်တိုင် ဖတ်ပြီး fix ကြည့်ပါ။

လေ့ကျင့်ခန်းများ

Task 1: Shape ဆိုတဲ့ protocol ကို var area: Double { get } requirement နဲ့ ဆောက်ပြီး Circle နဲ့ Rectangle struct နှစ်ခုကို conform လုပ်ကြည့်ပါ။ Task 2: [Circle] array တစ်ခု ဆောက်ပြီး closure-based sorted(by:) သုံးကာ area ငယ်ကနေ ကြီးအထိ sort လုပ်ပါ။ Task 3: NetworkResult ဆိုတဲ့ enum ကို success(data: String) နဲ့ failure(reason: String) associated value နှစ်ခုနဲ့ ဆောက်ပြီး switch statement နဲ့ case နှစ်ခုစလုံးကို handle ပါ။ Task 4: validateAge(_ age: Int) throws -> Int function ရေးပြီး age 0 အောက် (သို့) 120 အထက်ဆိုရင် custom Error enum (InvalidAgeError) throw ကာ do-catch နဲ့ handle ပါ။

Code နမူနာ

swift
// Task 1
protocol Shape {
    var area: Double { get }
}

struct Circle: Shape {
    let radius: Double
    var area: Double {
        // TODO: implement
        return 0
    }
}

struct Rectangle: Shape {
    let width: Double
    let height: Double
    var area: Double {
        // TODO: implement
        return 0
    }
}

// Task 2
let circles = [Circle(radius: 3), Circle(radius: 1), Circle(radius: 2)]
// TODO: sorted(by:) closure

// Task 3
enum NetworkResult {
    case success(data: String)
    case failure(reason: String)
}
// TODO: switch statement

// Task 4
enum InvalidAgeError: Error {
    case outOfRange
}
func validateAge(_ age: Int) throws -> Int {
    // TODO: implement
    return age
}
You should see
Circle/Rectangle area calculation တွေ correct ဖြစ်ပြီး circles array က radius order (1, 2, 3) အတိုင်း sort ဖြစ်ကာ, validateAge(150) ခေါ်ရင် InvalidAgeError.outOfRange error ကို catch block က correct စွာ ဖမ်းရမှာဖြစ်ပါတယ်။

၅ မိနစ် စမ်းကြည့်

Task 3 ရဲ့ NetworkResult enum ထဲကို loading case တစ်ခု ထပ်ထည့်ပြီး switch statement ကို exhaustive ဖြစ်အောင် update လုပ်ကြည့်ပါ- 5 minute လောက် ကိုယ်တိုင် ကြိုးစားကြည့်ပါ။

သတိလေးတစ်ချက်

Associated value ပါတဲ့ enum ကို switch လုပ်တဲ့အခါ case အားလုံးကို cover ဖို့ လိုပါတယ် - default case ကို အလွယ်တကူ သုံးလိုက်ရင် enum ထဲ case အသစ်ထပ်ထည့်ချိန် compiler warning မပေးတော့ဘဲ logic bug ဖြစ်တတ်ပါတယ်။

Real-world usage

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

Exercise Set 2: Struct, Protocol & Error Handling Practice example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

Mini challenge

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

Exercise Set 2: Struct, Protocol & Error Handling Practice 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
// Task 1
protocol Shape {
    var area: Double { get }
}

struct Circle: Shape {
    let radius: Double
    var area: Double {
        // TODO: implement
        return 0
    }
}

struct Rectangle: Shape {
    let width: Double
    let height: Double
    var area: Double {
        // TODO: implement
        return 0
    }
}

// Task 2
let circles = [Circle(radius: 3), Circle(radius: 1), Circle(radius: 2)]
// TODO: sorted(by:) closure

// Task 3
enum NetworkResult {
    case success(data: String)
    case failure(reason: String)
}
// TODO: switch statement

// Task 4
enum InvalidAgeError: Error {
    case outOfRange
}
func validateAge(_ age: Int) throws -> Int {
    // TODO: implement
    return age
}

ရလဒ်: Exercise Set 2: Struct, Protocol & Error Handling Practice ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

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

Exercise Set 2: Struct, Protocol & Error Handling Practice example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

Quick Quiz 1 / 3

Exercise Set 2: Struct, Protocol & Error Handling Practice ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • လိုအပ်ချက် → စဉ်းစားပြီး implement → စမ်းသပ်ပြီးရှင်းပြ
  • Exercise Set 2: Struct, Protocol & Error Handling Practice ကိုUI, domain state, persistence/service နဲ့tests ခွဲထားသောvertical slices ဖြင့်လုပ်ပါ။ Normal, boundary, nil, error, cancellation နဲ့concurrency paths ကိုrepeatable tests ဖြင့်စစ်ပြီးmain-actor work ကိုမပိတ်ပါနှင့်။
  • protocol requirement ကို { get } declare ထားပေမယ့် implement လုပ်တဲ့အခါ stored property အနေနဲ့ ထားပြီး computed property logic ကို လုံးဝ မထည့်တတ်ကြတယ်
  • Exercise Set 2: Struct, Protocol & Error Handling Practice 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

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

  • protocol requirement ကို { get } declare ထားပေမယ့် implement လုပ်တဲ့အခါ stored property အနေနဲ့ ထားပြီး computed property logic ကို လုံးဝ မထည့်တတ်ကြတယ်
  • throws function ကနေ error catch လုပ်တဲ့အခါ switch-style pattern matching (catch InvalidAgeError.outOfRange) ကို မသုံးဘဲ general catch { } တစ်ခုတည်းနဲ့ error detail ဆုံးရှုံးတတ်ကြတယ်
  • Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။

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

Exercise Set 2: Struct, Protocol & Error Handling Practice example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Circle/Rectangle area calculation တွေ correct ဖြစ်ပြီး circles array က radius order (1, 2, 3) အတိုင်း sort ဖြစ်ကာ, validateAge(150) ခေါ်ရင် InvalidAgeError.outOfRange error ကို catch block က correct စွာ ဖမ်းရမှာဖြစ်ပါတယ်။

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

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

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

Exercise Set 2: Struct, Protocol & Error Handling Practice | Thuta Learning