Properties
Properties associate values with a particular class, structure, or enumeration. Stored properties store constant and variable values as part of an instance, whereas computed properties calculate (rather than store) a value. Computed properties are provided by classes, structures, and enumerations. Stored properties are provided only by classes and structures.
ํ๋กํผํฐ๋
- ์ ์ฅ ํ๋กํผํฐ๋ ๊ตฌ์กฐ์ฒด, ํด๋์ค ๋ด๋ถ์์ ์์ ๋๋ ๋ณ์์ ๊ฐ์ ์ ์ฅ
- ์ฐ์ฐ ํ๋กํผํฐ๋ ๊ตฌ์กฐ์ฒด, ํด๋์ค, ์ด๊ฑฐํ ๋ด๋ถ์์ ๊ฐ์ ๊ณ์ฐ
- ์ฐ์ฐ ํ๋กํผํฐ๋ var๋ก๋ง ์ ์ธํ ์ ์์ต๋๋ค.
- ์ฐ์ฐํ๋กํผํฐ๋ฅผ ์ฝ๊ธฐ์ ์ฉ์ผ๋ก๋ ๊ตฌํํ ์ ์์ง๋ง, ์ฐ๊ธฐ ์ ์ฉ์ผ๋ก๋ ๊ตฌํํ ์ ์์ต๋๋ค.
- ์ฝ๊ธฐ์ ์ฉ์ผ๋ก ๊ตฌํํ๋ ค๋ฉด get ๋ธ๋ญ๋ง ์์ฑํด์ฃผ๋ฉด ๋ฉ๋๋ค. ์ฝ๊ธฐ์ ์ฉ์ get๋ธ๋ญ์ ์๋ตํ ์ ์์ต๋๋ค.
- ์ฝ๊ธฐ, ์ฐ๊ธฐ ๋ชจ๋ ๊ฐ๋ฅํ๊ฒ ํ๋ ค๋ฉด get ๋ธ๋ญ๊ณผ set๋ธ๋ญ์ ๋ชจ๋ ๊ตฌํํด์ฃผ๋ฉด ๋ฉ๋๋ค.
- set ๋ธ๋ญ์์ ์์์ ๋งค๊ฐ๋ณ์ newValue๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ํ๋กํผํฐ์ข ๋ฅ
- Stored property / Computed property
- instance property / type property
- Lazy Stored Properties : A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy modifier before its declaration.
ํ๋กํผํฐ ๊ตฌํ
struct Student {
// ์ธ์คํด์ค ์ ์ฅ ํ๋กํผํฐ
var name: String = ""
var koreanAge: Int = 0
// ์ธ์คํด์ค ์ฐ์ฐ ํ๋กํผํฐ
var westernAge: Int {
get {
return koreanAge - 1
}
set(inputValue) {
koreanAge = inputValue + 1
}
}
// ํ์
์ ์ฅ ํ๋กํผํฐ
static var typeDescription: String = "ํ์"
// ์ฝ๊ธฐ์ ์ฉ ํ์
์ฐ์ฐ ํ๋กํผํฐ
// ์ฝ๊ธฐ์ ์ฉ์์๋ get์ ์๋ตํ ์ ์์ต๋๋ค
static var selfIntroduction: String {
return "ํ์ํ์
์
๋๋ค"
}
}
var s1 = Student()
s1.name = "kim"
s1.koreanAge = 10
print(s1.westernAge) // 9
Property Observers
Property observers observe and respond to changes in a property’s value. Property observers are called every time a property’s value is set, even if the new value is the same as the property’s current value.
You can add property observers in the following places:
- Stored properties that you define
- Stored properties that you inherit
- Computed properties that you inherit
ํ๋กํผํฐ๊ฐ์์๋?
ํ๋กํผํฐ ๊ฐ์์๋ฅผ ์ฌ์ฉํ๋ฉด ํ๋กํผํฐ์ ๊ฐ์ด ๋ณ๊ฒฝ๋ ๋ ์ํ๋ ๋์์ ์ํํ ์ ์์ต๋๋ค.
๊ฐ์ด ๋ณ๊ฒฝ๋๊ธฐ ์ง์ ์ willSet๋ธ๋ญ์ด, ๊ฐ์ด ๋ณ๊ฒฝ๋ ์งํ์ didSet๋ธ๋ญ์ด ํธ์ถ๋ฉ๋๋ค.
๋ ์ค ํ์ํ ํ๋๋ง ๊ตฌํํด ์ฃผ์ด๋ ๋ฌด๊ดํฉ๋๋ค.
๋ณ๊ฒฝ๋๋ ค๋ ๊ฐ์ด ๊ธฐ์กด ๊ฐ๊ณผ ๋๊ฐ๋๋ผ๋ ํ๋กํผํฐ ๊ฐ์์๋ ํญ์ ๋์ํฉ๋๋ค.
willSet ๋ธ๋ญ์์๋ ์์์ ๋งค๊ฐ๋ณ์ newValue๋ฅผ, didSet ๋ธ๋ญ์์๋ oldValue๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ํ๋กํผํฐ ๊ฐ์์๋ ์ฐ์ฐ ํ๋กํผํฐ์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ํ๋กํผํฐ ๊ฐ์์๋ ํจ์, ๋ฉ์๋, ํด๋ก์ , ํ์ ๋ฑ์ ์ง์ญ/์ ์ญ ๋ณ์์ ๋ชจ๋ ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค.
ํ๋กํผํฐ๊ฐ์์ ๊ตฌํ
struct Money {
var currencyRate: Double = 1100 { // ํ๋กํผํฐ ๊ฐ์์ ์ฌ์ฉ
willSet(newRate) {
print("ํ์จ์ด \(currencyRate)์์ \(newRate)์ผ๋ก ๋ณ๊ฒฝ๋ ์์ ์
๋๋ค")
}
didSet(oldRate) {
print("ํ์จ์ด \(oldRate)์์ \(currencyRate)์ผ๋ก ๋ณ๊ฒฝ๋์์ต๋๋ค")
}
}
}
var moneyInMyPocket: Money = Money()
// ํ์จ์ด 1100.0์์ 1150.0์ผ๋ก ๋ณ๊ฒฝ๋ ์์ ์
๋๋ค
moneyInMyPocket.currencyRate = 1150
// ํ์จ์ด 1100.0์์ 1150.0์ผ๋ก ๋ณ๊ฒฝ๋์์ต๋๋ค
'iOS ๐ > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Swift13 - Initialization & Deinitialization (0) | 2021.02.02 |
---|---|
Swift12 - ์์ (0) | 2021.02.02 |
Swift10 - first class citizen (0) | 2021.02.02 |
Swift09 - ํด๋ก์ (0) | 2021.02.02 |
Swift08 - ๊ฐ ํ์ vs ์ฐธ์กฐํ์ (0) | 2021.02.01 |