Thuta Learning
Swift
ProjectsProgrammingအခြေခံ

Project: Task Manager App - Part 3 (Polish + Wrap Up)

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

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

  • Project: Task Manager App - Part 3 (Polish + Wrap Up) ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
  • Nil, error, boundary နဲ့concurrency cases စမ်းနိုင်ရန်
  • Safe, testable Swift code ရေးနိုင်ရန်

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

Project: Task Manager App - Part 3 (Polish + Wrap Up) ကို ရိုးရိုးလေးနားလည်မယ်

Part 3 က ဒီ project ရဲ့ နောက်ဆုံးအဆင့် ဖြစ်ပါတယ်။ Empty title ထည့်တာ၊ non-existent id ကို complete လုပ်ခိုင်းတာလို misuse case တွေကို graceful ဖြစ်စေဖို့ TaskError ဆိုတဲ့ custom Error enum ကို သတ်မှတ်ပြီး throws function တွေ ရေးမယ်။ addTask ကို validation ပါတဲ့ throwing function အဖြစ် ပြောင်းလဲပြီး do-catch ကို caller side မှာ သုံးပါမယ်။ နောက်ဆုံးအနေနဲ့ printSummary() method တစ်ခု ထည့်ပြီး done/pending count ကို priority အလိုက် group လုပ်ကာ report format ထုတ်ပေးမယ်။ ဒီအဆင့်ပြီးရင် error-handling, closures, struct/class, enum topic အားလုံးကို တစ်ပါတည်း ပေါင်းစပ်ထားတဲ့ project တစ်ခု ပြီးစီးသွားပါလိမ့်မယ်။

Problem → Why → Solution

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

throws function ကို try ပါမထားဘဲ ခေါ်ပြီး compile error ဖြစ်တာကို နားမလည်တတ်ကြတယ်

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

catch block တွေရေးတဲ့အခါ specific case ကို အရင်မစစ်ဘဲ general catch { } ကို အပေါ်ဆုံးမှာ ထားလိုက်လို့ specific catch code ဘယ်တော့မှ run မဖြစ်တတ်ကြတယ်

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

Project: Task Manager App - Part 3 (Polish + Wrap Up) ကိုUI, domain state, persistence/service နဲ့tests ခွဲထားသောvertical slices ဖြင့်လုပ်ပါ။ Normal, boundary, nil, error, cancellation နဲ့concurrency paths ကိုrepeatable tests ဖြင့်စစ်ပြီးmain-actor work ကိုမပိတ်ပါနှင့်။

Mental Model
  1. Project requirements
  2. Components + state + services
  3. စမ်းသပ်ပြီး UI

Project: Task Manager App - Part 3 (Polish + Wrap Up) ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

swift
enum TaskError: Error {
    case emptyTitle
    case taskNotFound(id: Int)
}

extension TaskManager {
    func addTask(title: String, priority: Priority) throws {
        guard !title.trimmingCharacters(in: .whitespaces).isEmpty else {
            throw TaskError.emptyTitle
        }
        let newTask = Task(id: tasks.count + 1, title: title, priority: priority)
        tasks.append(newTask)
    }

    func completeTask(id: Int) throws {
        guard let index = tasks.firstIndex(where: { $0.id == id }) else {
            throw TaskError.taskNotFound(id: id)
        }
        tasks[index].isDone = true
    }

    func printSummary() {
        print("===== Summary =====")
        print("Total: \(tasks.count), Done: \(tasks.count - pendingCount), Pending: \(pendingCount)")
        for p in [Priority.high, .medium, .low] {
            let count = tasks.filter { $0.priority == p && !$0.isDone }.count
            print("\(p.rawValue) pending: \(count)")
        }
    }
}

do {
    try manager.addTask(title: "Error handling ကျက်မယ်", priority: .high)
    try manager.completeTask(id: 99)
} catch TaskError.emptyTitle {
    print("Title လွတ်နေလို့ task ထည့်လို့ မရပါ")
} catch TaskError.taskNotFound(let id) {
    print("Task id #\(id) ကို ရှာမတွေ့ပါ")
} catch {
    print("Unknown error: \(error)")
}

manager.printSummary()

Project: Task Manager App - Part 3 (Polish + Wrap Up) ကိုUI, domain state, persistence/service နဲ့tests ခွဲထားသောvertical slices ဖြင့်လုပ်ပါ။ Normal, boundary, nil, error, cancellation နဲ့concurrency paths ကိုrepeatable tests ဖြင့်စစ်ပြီးmain-actor work ကိုမပိတ်ပါနှင့်။

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

  1. Project requirementsenum TaskError: Error {

    Part 3 က ဒီ project ရဲ့ နောက်ဆုံးအဆင့် ဖြစ်ပါတယ်။ Empty title ထည့်တာ၊ non-existent id ကို complete လုပ်ခိုင်းတာလို misuse case တွေကို graceful ဖြစ်စေဖို့ TaskError ဆိုတဲ့ custom Error enum ကို သတ်မှတ်ပြီး throws function တွေ ရေးမယ်။ addTask ကို validation ပါတဲ့ throwing function အဖြစ် ပြောင်းလဲပြီး do-catch ကို caller side မှာ သုံးပါမယ်။ နောက်ဆုံးအနေနဲ့ printSummary() method တစ်ခု ထည့်ပြီး done/pending count ကို priority အလိုက် group လုပ်ကာ report format ထုတ်ပေးမယ်။ ဒီအဆင့်ပြီးရင် error-handling, closures, struct/class, enum topic အားလုံးကို တစ်ပါတည်း ပေါင်းစပ်ထားတဲ့ project တစ်ခု ပြီးစီးသွားပါလိမ့်မယ်။

  2. Components + state + servicesprint("===== Summary =====")

    Project: Task Manager App - Part 3 (Polish + Wrap Up) ကိုUI, domain state, persistence/service နဲ့tests ခွဲထားသောvertical slices ဖြင့်လုပ်ပါ။ Normal, boundary, nil, error, cancellation နဲ့concurrency paths ကိုrepeatable tests ဖြင့်စစ်ပြီးmain-actor work ကိုမပိတ်ပါနှင့်။

  3. စမ်းသပ်ပြီး UImanager.printSummary()

    Project: Task Manager App - Part 3 (Polish + Wrap Up) example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

Execution Flow
  1. Project requirements
  2. Components + state + services
  3. စမ်းသပ်ပြီး UI

Success

Project: Task Manager App - Part 3 (Polish + Wrap Up) ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

Failure

throws function ကို try ပါမထားဘဲ ခေါ်ပြီး compile error ဖြစ်တာကို နားမလည်တတ်ကြတယ်

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

Part 3 က ဒီ project ရဲ့ နောက်ဆုံးအဆင့် ဖြစ်ပါတယ်။ Empty title ထည့်တာ၊ non-existent id ကို complete လုပ်ခိုင်းတာလို misuse case တွေကို graceful ဖြစ်စေဖို့ TaskError ဆိုတဲ့ custom Error enum ကို သတ်မှတ်ပြီး throws function တွေ ရေးမယ်။ addTask ကို validation ပါတဲ့ throwing function အဖြစ် ပြောင်းလဲပြီး do-catch ကို caller side မှာ သုံးပါမယ်။ နောက်ဆုံးအနေနဲ့ printSummary() method တစ်ခု ထည့်ပြီး done/pending count ကို priority အလိုက် group လုပ်ကာ report format ထုတ်ပေးမယ်။ ဒီအဆင့်ပြီးရင် error-handling, closures, struct/class, enum topic အားလုံးကို တစ်ပါတည်း ပေါင်းစပ်ထားတဲ့ project တစ်ခု ပြီးစီးသွားပါလိမ့်မယ်။

လက်တွေ့ ဆောက်ကြည့်မယ်

TaskError enum ကို emptyTitle, taskNotFound(id: Int) case နှစ်ခုနဲ့ Error protocol conform လုပ်ပြီး define ပါ။ addTask(title:priority:) ကို throws function ပြောင်းပြီး title.isEmpty ဖြစ်ရင် TaskError.emptyTitle throw ပါ။ completeTask(id:) ကိုလည်း throws ပြောင်းပြီး matching task မတွေ့ရင် TaskError.taskNotFound throw ပါ။ printSummary() method ရေးပြီး total, done, pending count နဲ့ priority တစ်ခုချင်းစီရဲ့ pending count ကို print ထုတ်ပါ။ main code ထဲမှာ do-catch block သုံးပြီး error case နှစ်ခုစလုံးကို handle ပြီး error message user-friendly ဖြစ်အောင် print ပါ။

Code နမူနာ

swift
enum TaskError: Error {
    case emptyTitle
    case taskNotFound(id: Int)
}

extension TaskManager {
    func addTask(title: String, priority: Priority) throws {
        guard !title.trimmingCharacters(in: .whitespaces).isEmpty else {
            throw TaskError.emptyTitle
        }
        let newTask = Task(id: tasks.count + 1, title: title, priority: priority)
        tasks.append(newTask)
    }

    func completeTask(id: Int) throws {
        guard let index = tasks.firstIndex(where: { $0.id == id }) else {
            throw TaskError.taskNotFound(id: id)
        }
        tasks[index].isDone = true
    }

    func printSummary() {
        print("===== Summary =====")
        print("Total: \(tasks.count), Done: \(tasks.count - pendingCount), Pending: \(pendingCount)")
        for p in [Priority.high, .medium, .low] {
            let count = tasks.filter { $0.priority == p && !$0.isDone }.count
            print("\(p.rawValue) pending: \(count)")
        }
    }
}

do {
    try manager.addTask(title: "Error handling ကျက်မယ်", priority: .high)
    try manager.completeTask(id: 99)
} catch TaskError.emptyTitle {
    print("Title လွတ်နေလို့ task ထည့်လို့ မရပါ")
} catch TaskError.taskNotFound(let id) {
    print("Task id #\(id) ကို ရှာမတွေ့ပါ")
} catch {
    print("Unknown error: \(error)")
}

manager.printSummary()
You should see
id #99 ကို ရှာမတွေ့လို့ "Task id #99 ကို ရှာမတွေ့ပါ" ဆိုတဲ့ message print ထွက်ပြီး၊ printSummary() ကနေ priority အလိုက် pending count ပါတဲ့ report ကို ထုတ်ပေးပါမယ်။

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

TaskError မှာ duplicateTitle case တစ်ခု ထပ်ထည့်ပြီး addTask မှာ title တူညီတဲ့ task ရှိပြီးသားဆိုရင် error throw ဖြစ်အောင် ကိုယ်တိုင် ရေးကြည့်ပါ- 5 minute လောက် စမ်းကြည့်ပါ။

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

catch block တွေရေးတဲ့အခါ order အရေးကြီးပါတယ် - general catch { } ကို အမြဲနောက်ဆုံးမှာပဲ ထားရပါမယ်၊ မဟုတ်ရင် specific error case တွေကို ဘယ်တော့မှ မရောက်ဘဲ compile error တောင် ထွက်နိုင်ပါတယ်။

Real-world usage

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

Project: Task Manager App - Part 3 (Polish + Wrap Up) example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

Mini challenge

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

Project: Task Manager App - Part 3 (Polish + Wrap Up) 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
enum TaskError: Error {
    case emptyTitle
    case taskNotFound(id: Int)
}

extension TaskManager {
    func addTask(title: String, priority: Priority) throws {
        guard !title.trimmingCharacters(in: .whitespaces).isEmpty else {
            throw TaskError.emptyTitle
        }
        let newTask = Task(id: tasks.count + 1, title: title, priority: priority)
        tasks.append(newTask)
    }

    func completeTask(id: Int) throws {
        guard let index = tasks.firstIndex(where: { $0.id == id }) else {
            throw TaskError.taskNotFound(id: id)
        }
        tasks[index].isDone = true
    }

    func printSummary() {
        print("===== Summary =====")
        print("Total: \(tasks.count), Done: \(tasks.count - pendingCount), Pending: \(pendingCount)")
        for p in [Priority.high, .medium, .low] {
            let count = tasks.filter { $0.priority == p && !$0.isDone }.count
            print("\(p.rawValue) pending: \(count)")
        }
    }
}

do {
    try manager.addTask(title: "Error handling ကျက်မယ်", priority: .high)
    try manager.completeTask(id: 99)
} catch TaskError.emptyTitle {
    print("Title လွတ်နေလို့ task ထည့်လို့ မရပါ")
} catch TaskError.taskNotFound(let id) {
    print("Task id #\(id) ကို ရှာမတွေ့ပါ")
} catch {
    print("Unknown error: \(error)")
}

manager.printSummary()

ရလဒ်: Project: Task Manager App - Part 3 (Polish + Wrap Up) ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

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

Project: Task Manager App - Part 3 (Polish + Wrap Up) example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

Quick Quiz 1 / 3

Project: Task Manager App - Part 3 (Polish + Wrap Up) ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Project requirements → Components + state + services → စမ်းသပ်ပြီး UI
  • Project: Task Manager App - Part 3 (Polish + Wrap Up) ကိုUI, domain state, persistence/service နဲ့tests ခွဲထားသောvertical slices ဖြင့်လုပ်ပါ။ Normal, boundary, nil, error, cancellation နဲ့concurrency paths ကိုrepeatable tests ဖြင့်စစ်ပြီးmain-actor work ကိုမပိတ်ပါနှင့်။
  • throws function ကို try ပါမထားဘဲ ခေါ်ပြီး compile error ဖြစ်တာကို နားမလည်တတ်ကြတယ်
  • Project: Task Manager App - Part 3 (Polish + Wrap Up) 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

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

  • throws function ကို try ပါမထားဘဲ ခေါ်ပြီး compile error ဖြစ်တာကို နားမလည်တတ်ကြတယ်
  • catch block တွေရေးတဲ့အခါ specific case ကို အရင်မစစ်ဘဲ general catch { } ကို အပေါ်ဆုံးမှာ ထားလိုက်လို့ specific catch code ဘယ်တော့မှ run မဖြစ်တတ်ကြတယ်
  • Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။

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

Project: Task Manager App - Part 3 (Polish + Wrap Up) example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: id #99 ကို ရှာမတွေ့လို့ "Task id #99 ကို ရှာမတွေ့ပါ" ဆိုတဲ့ message print ထွက်ပြီး၊ printSummary() ကနေ priority အလိုက် pending count ပါတဲ့ report ကို ထုတ်ပေးပါမယ်။

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

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

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

Project: Task Manager App - Part 3 (Polish + Wrap Up) | Thuta Learning