Type Alias
- 기본 자료형에 새로운 이름(별칭)을 부여하여 사용하고 코드를 좀 더 읽기 쉽고 이해하기 쉽도록 만드는 문법이다.
- typealias 키워드를 통해서 별칭을 생성할 수 있다.
- 특정 타입에 대한 이름(별칭)이 선언된 뒤에 프로그램 내에서 기존 타입을 대신해 사용할 수 있다.
- typealias는 새로운 타입을 만드는것이 아닌, 기존 타입을 참조하는 것이다.
// 기본 문법
typealias name(별명) = existing type (기존에 있는 타입)
스위프트에서 typealias는 대부분의 유형에서 사용할 수 있는데 크게 5가지 유형에서 사용할 수 있다.
- Built-in Types(String, Int, Double ... )
- User Defined Types(Class, Struct, enum)
- Complex Type(Closures)
- protocol
- generic
Built-in Types
String, Int, Double 등과 같은 모든 내장 데이터 타입에 대해 사용할 수 있다.
// typealias 사용 X
let name: String = "Parkkiwoo"
print(type(of: name)) // String
// -----------------------------
// typealias 사용
typealias FullName = String
let name: FullName = "Parkkiwoo"
print(type(of: name)) // String
User Defined Types
코드를 작성하면서 고유한 데이터 타입(Class, Struct, Enum)을 만드는 경우에도 사용할 수 있다.
class Student {
let name: String
let age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
}
// typealias 사용 X
var students: [Student] = []
print(type(of: students)) // Array<Student>
// ------------------------------------------
// typealias 사용
typealias Students = [Student]
var students: Students = []
print(type(of: students)) // Array<Student>
Complex Type
클로저를 입력 매개변수로 사용하는 메서드에서도 사용할 수 있다.
// typealias 사용 X
func typealiasClosures(completion: (Int) -> (String)) -> String {
completion(999)
}
var result: String = typealiasClosures { number in
return "\(number)"
}
print(result)
// ----------------------------------------------------------------
// typealias 사용
typealias CompletionHandler = (Int) -> (String)
func typealiasClosures(completion: CompletionHandler) -> String {
completion(999)
}
var result = typealiasClosures { number in
return "\(number)"
}
print(result)
Protocol
// typealias 사용 X
protocol StudentName {
func getName() -> String
}
protocol StudentAge {
func getAge() -> Int
}
struct Student: StudentName, StudentAge {
func getName() -> String {
return "name"
}
func getAge() -> Int {
return 999
}
}
// ---------------------------------------
// typealias 사용
protocol StudentName {
func getName() -> String
}
protocol StudentAge {
func getAge() -> Int
}
typealias StudentInfo = StudentName & StudentAge
struct Student: StudentInfo {
func getName() -> String {
return "name"
}
func getAge() -> Int {
return 999
}
}
generic
// typealias 사용 X
func location<T>(_ point: T ) {
print(point)
}
location((1.11, 2.22))
// ---------------------------------------------------
// typealias 사용
typealias LocationPoint<T> = (T,T)
func location<LocationPoint>(_ point: LocationPoint) {
print(point)
}
location((1.11, 2.22))
참조자료
Swift - typealias
안녕하세요. brody입니다. Swift에서 사용하는 typealias에 대한 내용을 정리했어요. 저는 type alias의 뜻을 하나씩 번역하면 '타입 별명'으로 타입에 별명을 붙여서 쉽게 사용한다~ 라고만
brody.tistory.com
https://www.programiz.com/swift-programming/typealias
Swift Typealias: How to use them and Why?
A type alias allows you to provide a new name for an existing data type into your program. After a type alias is declared, the aliased name can be used instead of the existing type throughout the program. Type alias do not create new types. They simply pro
www.programiz.com
'Swift > Swift문법' 카테고리의 다른 글
| Swift문법 - Conditional Statements(if, switch, guard) (0) | 2022.12.10 |
|---|---|
| Swift문법 - Operator (0) | 2022.11.27 |
| Swift문법 - Type Conversion (0) | 2022.11.13 |
| Swift문법 - Type Inference and Type Annotation (0) | 2022.10.09 |
| Swift문법 - Data Types (0) | 2022.10.02 |