๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
iOS ๐ŸŽ/Swift

Swift11 - ํ”„๋กœํผํ‹ฐ

by yongmin.Lee 2021. 2. 2.

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