Thuta Learning
Swift
AdvancedProgrammingအခြေခံ

Structs vs Classes

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

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

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

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

Structs vs Classes ကို ရိုးရိုးလေးနားလည်မယ်

Swift မှာ custom data model တည်ဆောက်ဖို့ struct နဲ့ class ကိုအသုံးများပါတယ်။ Struct က value type ဖြစ်ပြီး copy လုပ်တဲ့အခါ data အသစ်တစ်ခုဖြစ်သွားပါတယ်။ Class က reference type ဖြစ်ပြီး copy လုပ်တဲ့အခါ object တစ်ခုတည်းကိုပဲ reference လုပ်နေတတ်ပါတယ်။

Problem → Why → Solution

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

Class property ကို initial value မပေးဘဲ initializer ထဲမှာလည်းမသတ်မှတ်ရင် compile error ဖြစ်နိုင်ပါတယ်။

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

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

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

Structs vs Classes ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။

Mental Model
  1. Value / reference
  2. Protocol + type composition
  3. Reusable behavior

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

အဓိက Syntax / Pattern

swift
struct Course {
    var title: String
    var lessonCount: Int
}

class Student {
    var name: String
    var enrolledCourse: Course?

    init(name: String) {
        self.name = name
    }
}

let swiftCourse = Course(title: "Swift Basics", lessonCount: 20)
let student = Student(name: "Nandar")
student.enrolledCourse = swiftCourse

print(student.name)
print(student.enrolledCourse?.title ?? "No course")

Structs vs Classes ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။

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

  1. Value / referencestruct Course {

    Swift မှာ custom data model တည်ဆောက်ဖို့ struct နဲ့ class ကိုအသုံးများပါတယ်။ Struct က value type ဖြစ်ပြီး copy လုပ်တဲ့အခါ data အသစ်တစ်ခုဖြစ်သွားပါတယ်။ Class က reference type ဖြစ်ပြီး copy လုပ်တဲ့အခါ object တစ်ခုတည်းကိုပဲ reference လုပ်နေတတ်ပါတယ်။

  2. Protocol + type compositionself.name = name

    Structs vs Classes ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။

  3. Reusable behaviorprint(student.enrolledCourse?.title ?? "No course")

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

Execution Flow
  1. Value / reference
  2. Protocol + type composition
  3. Reusable behavior

Success

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

Failure

Class property ကို initial value မပေးဘဲ initializer ထဲမှာလည်းမသတ်မှတ်ရင် compile error ဖြစ်နိုင်ပါတယ်။

Swift မှာ custom data model တည်ဆောက်ဖို့ struct နဲ့ class ကိုအသုံးများပါတယ်။ Struct က value type ဖြစ်ပြီး copy လုပ်တဲ့အခါ data အသစ်တစ်ခုဖြစ်သွားပါတယ်။ Class က reference type ဖြစ်ပြီး copy လုပ်တဲ့အခါ object တစ်ခုတည်းကိုပဲ reference လုပ်နေတတ်ပါတယ်။

App model တွေ၊ simple data container တွေမှာ struct ကိုအရင်စဉ်းစားတာကောင်းပါတယ်။ Shared mutable state, inheritance လိုတဲ့နေရာတွေမှာ class ကိုသုံးနိုင်ပါတယ်။

swift
struct Course {
    var title: String
    var lessonCount: Int
}

class Student {
    var name: String
    var enrolledCourse: Course?

    init(name: String) {
        self.name = name
    }
}

let swiftCourse = Course(title: "Swift Basics", lessonCount: 20)
let student = Student(name: "Nandar")
student.enrolledCourse = swiftCourse

print(student.name)
print(student.enrolledCourse?.title ?? "No course")

Course က simple data model ဖြစ်လို့ struct နဲ့ရေးထားပါတယ်။ Student က initializer ပါပြီး course ကိုနောက်မှချိတ်နိုင်အောင် class နဲ့ပြထားပါတယ်။ enrolledCourse? က course မရွေးထားသေးနိုင်လို့ optional ဖြစ်ပါတယ်။

You should see
Nandar Swift Basics

Real-world usage

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

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

Mini challenge

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

Structs vs Classes 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
struct Course {
    var title: String
    var lessonCount: Int
}

class Student {
    var name: String
    var enrolledCourse: Course?

    init(name: String) {
        self.name = name
    }
}

let swiftCourse = Course(title: "Swift Basics", lessonCount: 20)
let student = Student(name: "Nandar")
student.enrolledCourse = swiftCourse

print(student.name)
print(student.enrolledCourse?.title ?? "No course")

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

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

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

Quick Quiz 1 / 3

Structs vs Classes ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Value / reference → Protocol + type composition → Reusable behavior
  • Structs vs Classes ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။
  • Class property ကို initial value မပေးဘဲ initializer ထဲမှာလည်းမသတ်မှတ်ရင် compile error ဖြစ်နိုင်ပါတယ်။
  • Structs vs Classes 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

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

  • Class property ကို initial value မပေးဘဲ initializer ထဲမှာလည်းမသတ်မှတ်ရင် compile error ဖြစ်နိုင်ပါတယ်။
  • Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
  • Force unwrap သို့unchecked shared mutable state ဖြင့်compiler safety ကိုကျော်ခြင်း။

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

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

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Nandar Swift Basics

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

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

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

Structs vs Classes | Thuta Learning