Properties တွေက struct/class/enum ထဲမှာ data သိမ်းတဲ့ variable/constant တွေပါ။ Stored property က data ကိုတကယ်သိမ်းတယ်။ Computed property ကတော့ တခြား data တွေပေါ်မူတည်ပြီး တန်ဖိုးကိုတွက်ပြန်ပေးပါတယ်။
swift
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 ပြန်ပေးပါတယ်။
You should see
Pro Plan: $18.0