생성자
생성자(Constructor)
- 클래스, 구조체의 초기화 블록
- 초기화 구문(init)을 한번 정의하면 빈 초기화 ()를 사용할 수 없음
struct Resolution{ var width = 0 var height = 0 init(width: Int){ self.width = width } } let resolution = Resolution() (x) => 불가능 let resolution = Resolution(width:4096) (o)
디폴트 매개변수를 이용한 방법 struct Resolution{ var width = 0 var height = 0 init(width: Int = 0){ self.width = width } } let resolution = Resolution() (o) let resolution = Resolution(width:4096) (o)
- 상속받은 부모 클래스내의 정의를 초기화
``language class Base{ var baseValue : Double init(inputValue: Double){ self.baseValue = inputValue } } class ExBase : Base{ override init(inputValue:Double){ super.init(inputValue: 10.5) } }
초기화 구문 델리게이션(delegation)
- 체이닝으로 결국 첫 초기화 구문까지 도달
- override super.init(inputValue: ) -> override super.init(inputValue: ) -> override super.init(inputValue: ) -> init(inputValue: )
'IOS > Swift' 카테고리의 다른 글
enum (0) | 2016.05.28 |
---|---|
옵셔널의 단점, 옵셔널 체인 (0) | 2016.05.28 |
Any, AnyObject (0) | 2016.05.28 |
타입 캐스팅 (0) | 2016.05.28 |
상속, 오버라이딩 (0) | 2016.05.28 |
댓글 로드 중…
트랙백을 확인할 수 있습니다
URL을 배껴둬서 트랙백을 보낼 수 있습니다