가변 상태 (mutable state): 변경이 가능한 상태
불변 상태 (immutable state): 변경이 불가능한 상태
var variable: Int = 1
let constant: Int = 1
variable = 2
//constant = 2
// Cannot assign to value: 'constant' is a 'let' constant
클래스 또는 구조체의 인스턴스와 연관된 값을 저장하는 프로퍼티
struct Point {
var x = 0.0
let y = 0.0
}
var point = Point(x: 3.5)
point.x = 5.5
// 5.5, 0.0
//point.y = 7.5
// Cannot assign to property: 'y' is a 'let' constant
struct Point {
var x = 0.0, y = 0.0
// 값 타입(Value type)인 구조체와 열거형의 경우
// mutating 키워드를 앞에 붙여주어야
// 인스턴스 메소드 내부에서 프로퍼티의 값을 변경할 수 있음
mutating func moveBy(x deltaX: Double, y deltaY: Double) {
self.x += deltaX
self.y += deltaY
}
}