Extension
Extension이란
- 기존에 선언된 class, struct, enum, protocol을 확장하는 것
- 나만의 프로퍼티나 메서드를 만들고 싶을 때 사용한다
- 예) Int값의 2배를 반환하는 프로퍼티를 추가하고 싶을 때
extension Int {
var twice: Int { self * 2 }
}
print(2.twice)
Extension에서 할 수 있는 일
- Computed Property 추가
- 메서드 추가
- convenience init 추가
- subscript 추가
- class, struct, enum 추가
- protocol 채택
Extension에서 할 수 없는 일
- Stored Property 추가
- Property Observer 추가
- 기존 메서드 오버라이드
- designated init 추가
- deinit 추가