ရိုးရိုးလေး ရှင်းပြမယ်
Properties ကို ရိုးရိုးလေးနားလည်မယ်
Properties တွေက struct/class/enum ထဲမှာ data သိမ်းတဲ့ variable/constant တွေပါ။ Stored property က data ကိုတကယ်သိမ်းတယ်။ Computed property ကတော့ တခြား data တွေပေါ်မူတည်ပြီး တန်ဖိုးကိုတွက်ပြန်ပေးပါတယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Computed property ထဲမှာ သူ့ကိုယ်သူပြန်ခေါ်တဲ့ logic ရေးမိရင် infinite recursion ဖြစ်နိုင်ပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Properties ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။
- Typed value
- Swift expression / function
- Safe result
Properties ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
struct Product {
var name: String
var price: Double
var discountPercent: Double
var finalPrice: Double {
price - (price * discountPercent / 100)
}
}
let plan = Product(name: "Pro Plan", price: 20.0, discountPercent: 10)
print("\(plan.name): $\(plan.finalPrice)")Properties ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Typed value
struct Product {Properties တွေက struct/class/enum ထဲမှာ data သိမ်းတဲ့ variable/constant တွေပါ။ Stored property က data ကိုတကယ်သိမ်းတယ်။ Computed property ကတော့ တခြား data တွေပေါ်မူတည်ပြီး တန်ဖိုးကိုတွက်ပြန်ပေးပါတယ်။
- Swift expression / function
price - (price * discountPercent / 100)Properties ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။
- Safe result
print("\(plan.name): $\(plan.finalPrice)")Properties 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
Properties ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
Computed property ထဲမှာ သူ့ကိုယ်သူပြန်ခေါ်တဲ့ logic ရေးမိရင် infinite recursion ဖြစ်နိုင်ပါတယ်။
Properties တွေက struct/class/enum ထဲမှာ data သိမ်းတဲ့ variable/constant တွေပါ။ Stored property က data ကိုတကယ်သိမ်းတယ်။ Computed property ကတော့ တခြား data တွေပေါ်မူတည်ပြီး တန်ဖိုးကိုတွက်ပြန်ပေးပါတယ်။
struct Product {
var name: String
var price: Double
var discountPercent: Double
var finalPrice: Double {
price - (price * discountPercent / 100)
}
}
let plan = Product(name: "Pro Plan", price: 20.0, discountPercent: 10)
print("\(plan.name): $\(plan.finalPrice)")name, price, discountPercent တွေက stored properties ပါ။ finalPrice က computed property ဖြစ်ပြီး discount တွက်ပြီးမှ result ပြန်ပေးပါတယ်။
Pro Plan: $18.0Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Properties example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Properties 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 ကိုခန့်မှန်းပါ
struct Product {
var name: String
var price: Double
var discountPercent: Double
var finalPrice: Double {
price - (price * discountPercent / 100)
}
}
let plan = Product(name: "Pro Plan", price: 20.0, discountPercent: 10)
print("\(plan.name): $\(plan.finalPrice)")ရလဒ်: Properties ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Properties 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
- Properties ကိုsyntax အဖြစ်သာမမြင်ဘဲvalue/reference semantics, mutability, copying, identity, optionality နဲ့lifetime contract အဖြစ်နားလည်ပါ။ Struct/enum value types ကိုdefault စဉ်းစားပြီးclass identity နဲ့shared mutable state ကိုလိုမှသာသုံးပါ။
- Computed property ထဲမှာ သူ့ကိုယ်သူပြန်ခေါ်တဲ့ logic ရေးမိရင် infinite recursion ဖြစ်နိုင်ပါတယ်။
- Properties 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