self 프로퍼티 (The self Property)에 대해서 알아보자. Type(인간)의 모든 Property(피부색, 키, 몸무게)는 Instance(한가온) 자체를 의미하는 self Property를 가진다. Instance(한가온)의 Method(달리기, 밥먹기, 씻기) 안에서 self Property를 이용해 Instance(한가온) 자체에 접근해서 사용할 수 있다.
class Human { //Human이라는 클래스형 Type.
var name: String //name이라는 Property.
init(name: String) { //추후에 초기화를 통해 어떠한 Instance를 생성할 때
self.name = name //그 Instance 자체(self)의 name이라는 Property의 값은 (name: String) 안에 있는 name이다.
}
}
Self는 공식 Document에서 다루지 않아서 일단 구글링했다. self가 Type의 Instance를 가리킨다면, Self는 Type 그 자체를 가리킨다고 한다.
'Note' 카테고리의 다른 글
타입 프로퍼티 (0) | 2023.04.07 |
---|---|
익스텐션 (Extensions) (0) | 2023.04.06 |
Type, Instance, Property, Method, Initialization (0) | 2023.03.29 |
Spacer와 padding의 차이 (0) | 2023.03.28 |
ObservableObject, Publisher, Subscriber, @Published (0) | 2023.02.12 |