iOS 🍎/Swift

Swift11 - ν”„λ‘œνΌν‹°

yongmin.Lee 2021. 2. 2. 16:13

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으둜 λ³€κ²½λ˜μ—ˆμŠ΅λ‹ˆλ‹€