mutating

    [Swift] struct 내부에서 변수값 변경하기

    [Swift] struct 내부에서 변수값 변경하기

    struct의 immutability struct는 한 번 생성자를 초기화하면 struct 내부에서는 var 로 선언되더라도 프로퍼티의 값을 바꿀 수 없게된다. 예를 들어 아래와 같이 struct Person에 addAge를 하는 코드를 넣어본 후 실행해보자. struct Person { let name: String var age: Int let temperature: Float init(_name: String, _age: Int, _temperature : Float) { name = _name age = _age temperature = _temperature } func addAge() { age += 1 } } 아래와 같이 age가 immutable 하다는 warning이 뜬다. 이유는 초기화..