ရိုးရိုးလေး ရှင်းပြမယ်
Methods ကို ရိုးရိုးလေးနားလည်မယ်
Method က type တစ်ခုနဲ့ဆက်စပ်နေတဲ့ function ပါ။ Function က standalone ဖြစ်နိုင်ပေမယ့် method က struct/class/enum instance တစ်ခုရဲ့ behavior ကိုဖော်ပြပါတယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Struct ထဲမှာ property ပြောင်းမယ့် method ရေးရင် mutating လိုနိုင်ပါတယ်။ Class method တွေမှာတော့ ဒီလိုမလိုပါ။
ဖြစ်လေ့ရှိတဲ့အမှား
Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Methods ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- Typed value
- Swift expression / function
- Safe result
Methods ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
class Counter {
var count = 0
func increment() {
count += 1
}
func increment(by amount: Int) {
count += amount
}
func reset() {
count = 0
}
}
let counter = Counter()
counter.increment()
counter.increment(by: 5)
print(counter.count)Methods ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Typed value
class Counter {Method က type တစ်ခုနဲ့ဆက်စပ်နေတဲ့ function ပါ။ Function က standalone ဖြစ်နိုင်ပေမယ့် method က struct/class/enum instance တစ်ခုရဲ့ behavior ကိုဖော်ပြပါတယ်။
- Swift expression / function
func reset() {Methods ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- Safe result
print(counter.count)Methods example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
- Typed value
- Swift expression / function
- Safe result
Success
Methods ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
Struct ထဲမှာ property ပြောင်းမယ့် method ရေးရင် mutating လိုနိုင်ပါတယ်။ Class method တွေမှာတော့ ဒီလိုမလိုပါ။
Method က type တစ်ခုနဲ့ဆက်စပ်နေတဲ့ function ပါ။ Function က standalone ဖြစ်နိုင်ပေမယ့် method က struct/class/enum instance တစ်ခုရဲ့ behavior ကိုဖော်ပြပါတယ်။
class Counter {
var count = 0
func increment() {
count += 1
}
func increment(by amount: Int) {
count += amount
}
func reset() {
count = 0
}
}
let counter = Counter()
counter.increment()
counter.increment(by: 5)
print(counter.count)Counter class ထဲမှာ count state ရှိပြီး method တွေက အဲဒီ state ကိုပြောင်းပါတယ်။ increment() က 1 တိုးပြီး increment(by:) က ပေးလိုက်တဲ့ amount အတိုင်းတိုးပါတယ်။
6Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Methods example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Methods 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 ကိုခန့်မှန်းပါ
class Counter {
var count = 0
func increment() {
count += 1
}
func increment(by amount: Int) {
count += amount
}
func reset() {
count = 0
}
}
let counter = Counter()
counter.increment()
counter.increment(by: 5)
print(counter.count)ရလဒ်: Methods ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Methods example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Typed value → Swift expression / function → Safe result
- Methods ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- Struct ထဲမှာ property ပြောင်းမယ့် method ရေးရင် mutating လိုနိုင်ပါတယ်။ Class method တွေမှာတော့ ဒီလိုမလိုပါ။
- Methods 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