[Swift 문법] 값 타입과 참조 타입

!·2022년 6월 25일
0

Swift 문법

목록 보기
13/27

구조체 vs 클래스


struct ValueType{
	var property = 1
}

class ReferenceType{
	var property = 1
}

let firstStructInstance = ValueType()
var secondStructInstance = firstStructInstance
secondStructInstance.property = 2 

let firstClassInstance = ReferenceType()
var secondCLassInstance = firstReferenceInstance
secondClasInstance.property = 2

// 1,2 vs 2,2

스위프트의 데이터 타입

  • public struct Int
  • public struct Double
  • public struct String
  • public struct Dictionary<Key : Hashble, Value>
  • public struct Array<Element>
  • public struct Set<Element : Hashable>

스위프트와 구조체

  • 스위프트는 구조체, 열거형 사용을 선호한다.
  • Apple 프레임워크는 대부분 클래스 사용
  • Apple 프레임워크 사용시 구조체/클래스 선택은 본인의 몫!

배열과 딕셔너리 집합과 같은 경우는 함수의 인자로 전달할 때, 값 타입이 복사되기 때문에 데이터의 크기가 클수록 불리할 수도 있다. 하지만 스위프트에서는 데이터의 크기가 크다면 참조타입으로 변환해 전달함으로 이에 대해 신경쓰지 않아도 된다.

profile
개발자 지망생

0개의 댓글