iOS/Swift

[Swift] 구조체와 클래스(Structures and Classes)

Dev.Andy 2023. 5. 17. 23:53

머리말

참고 자료

Swift 영문 공식 문서 - Structures and Classes

Structures and Classes | Documentation

Swift 한글 공식 문서 - 구조체와 클래스(Structures and Classes)

구조체와 클래스 (Structures and Classes) - Swift

 

공통점과 차이점

차이점

  구조체 클래스
⭐️ 타입 Value Type Reference Type
메모리 저장 - in Stack
- Stack Frame이 종료 시 자동 제거됨
- in Heap
- ARC에 의해 자동으로 메모리 관리
인스턴스의 저장 모든 데이터 in Stack - 인스턴스 데이터 in Heap
- 변수 in Stack
복사본 생성 값을 할당할 때마다 새로운 메모리 공간을 생성 값을 전달하지 않고 저장된 주소를 전달

 

인스턴스(Instances)

인스턴스 용어의 사용 vs 객체

  • 일반적으로 프로그래밍 언어에서 클래스의 인스턴스를 객체(object)라 부르지만, Swift의 구조체와 클래스 챕터에서는 두 개념의 기능이 많이 가깝기 때문에 객체라 부르지 않고 인스턴스(instance) 용어를 더 사용한다.

Note
An instance of a class is traditionally known as an object. However, Swift structures and classes are much closer in functionality than in other languages, and much of this chapter describes functionality that applies to instances of either a class or a structure type. Because of this, the more general term instance is used.

인스턴스 의미

Custom Type을 정의한 이후에, 실제로 메모리에 할당되어 구체적인 실체를 갖춘 데이터