Open yoogail105 opened 2 years ago
→ 같은 클래스 인스턴스를 여러개의 변수에 할당한 뒤, 값을 변경시키면 할당한 모든 변수에 영향을 준다.
class classGreeting {
var message = "Hello, class playground"
}
var firstGreeting = classGreeting()
var secondGreeting = firstGreeting
var thirdGreeting = firstGreeting
firstGreeting.message
secondGreeting.message = "hello"
firstGreeting.message
secondGreeting.message
thirdGreeting.message
초기화 구문 작성 init()
class Shirt {
var size: Int
var color: String
var brand: String
init(size: Int, color: String, brand: String){
self.size = size
self.color = color
self.brand = brand
}
}
var blueShirt = Shirt(size: 66, color: "blue", brand: "8Seconds")
값 타입 call by value
→ 구조체의 인스턴스를 또 다른 변수에 대입할 경우, 복사되어 새로운 인스턴스가 생성되기 때문에 기존에 생성된 인스턴스와 독립적으로 동작한다.
자동 생성 초기화 memberwise init
memberwise init
을 통해서 초기화구문이 생성된다.
swift의 기본 자료 구조 블럭: 클래스, 구조체, 열거형
class, struct의 공통점
init()
을 통해 초기화 구문을 작성해주어야하지만 struct는memberwise init
을 통해 자동 초기화🔖 참고