Value Type
A value type is a type whose value is copied when it’s assigned to a variable or constant, or when it’s passed to a function.
변수에 할당되거나 함수로 넘길 때에 기존 structure 삭제 후 값만을 넘겨주어 다시 할당한다.
(enum, array, string 모두 value type의 변수이다.)
Immutability
let으로 선언된 struct 변수는 property 변경이 불가능하다.
또한 struct 내부에서 선언된 메소드는 기본적으로 self property를 변경할 수 없다.
self property를 var로 선언하고 func 앞에 mutating 키워드를 붙여주면 가능하다.
protocol 적용은 가능하고 상속은 불가능하다.
Referenece Type
Unlike value types, reference types are not copied when they’re assigned to a variable or constant, or when they’re passed to a function. Rather than a copy, a reference to the same existing instance is used.
변수에 할당되거나 함수로 넘길 때에 기존 class 변수의 주소 값을 전달한다.
주소 값을 전달하기 때문에 주소 값을 넘겨받은 객체 또한 본 객체와 같은 영향을 받는다.
let으로 선언된 class 변수도 property 변경이 가능하다.
protocol 적용과 상속이 가능하다.
origin: https://developer.apple.com/documentation/swift/choosing_between_structures_and_classes
Apple 공식문서에 따르면, 기본적으로 structure를 사용할 것을 권장하고 Objective-c와 사용하거나 상속이 필요한 경우 class를 사용하라고 한다.