swift 공식 문서
[iOS - swift 공식 문서] 9. Structures and Classes
jake-kim
2021. 6. 25. 22:25
Structures and Classes
- swift에서는 class의 인스턴스를 object라고 하지 않고, 기능에 가까운 언어이므로 "instance"라고 명명
- Class에서만 있는 속성
- 상속
- type casting: 런타임에 클래스 instance의 유형을 확인하고 해석
- Deinitialzer는 클래스의 instance가 할당 된 리소스를 해제할 수 있도록 하는 기능
- reference counting을 통해 한 클래스 instance에 대한 하나 이상의 참조를 허용
Structure와 Enum은 value type
- copy - by - value
let hd = Resolution(width: 1920, height: 1080)
var cinema = hd
cinema.width = 2048 // copy - by - value
cinema.width // 2048
hd.width // 1920
Identity Operator
- class는 reference type이기 때문에 동일한 단일 instance를 참조 가능
- Identity Operator는 참조하는 대상 instance를 비교하는 연산자
- 두 가지 존재: ===, !==
- 일반적으로 reference type을 비교할때는 compile error 발생

- Identity Operator 사용하여 비교
print(referenceFirst === referenceSecond) // true