Referenced By

Swift) 프로퍼티 정복하기 (2/4) - 연산 프로퍼티(Computed Property)


연산 프로퍼티 특징

코드 예제

class Human {
    
    var name: String = "Yoonds"
    
    var computed: String {
        get {
            return "연산 프로퍼티 get을 활용한 " + self.name
        }
        
        set {
            self.name = "연산 프로퍼티 set을 활용한 새로운 값 할당: " + newValue
        }
    }
    
}

var human: Human = .init()

// get 호출
print(human.computed) // get을 활용한 Yoonds

// set 값 할당
human.computed = "새로운 값!"
print(human.name) // set을 활용한 새로운 값 할당: 새로운 값!

궁금한 부분