Link
Notice
HIT해
[Swift 기초문법 - 51] 중첩 타입 Nested Type 본문
728x90
Nested Type이란?
sturct나 enum 안에 다른 타입을 넣는 걸 뜻한다.
예시
struct MyPet {
enum Kind {
case cat
case dog
var value : String {
// 자기 자신을 switch구문으로 정의할 수 있다.
switch self {
case .cat :
return "고양이"
case .dog :
return "강아지"
}
}
} // Kind enum
}
value를 중첩타입이라고 부른다
struct MyPet {
enum Kind {
case cat
case dog
var value : String {
// 자기 자신을 switch구문으로 정의할 수 있다.
switch self {
case .cat :
return "고양이"
case .dog :
return "강아지"
}
}
} // Kind enum
let kind : Kind
var description : String {
return "우리의 \(kind.value)"
}
}
let myCat = MyPet(kind: .cat)
print(myCat.description)
let myDog = MyPet(kind: .dog)
print(myDog.description)
if myCat.kind == MyPet.kind.dog {
print("고양이가 아닙니다")
}
정리하자면
타입 자체를 enum으로 만들려고 할 때 외부에 만들려고 하면 많아질 수 있어 위와같은 방식으로 만든 것을 Nested Type이라 한다
'Swift > 기초문법' 카테고리의 다른 글
[Swift 기초문법 - 53] Subset Superset (0) | 2024.08.19 |
---|---|
[Swift 기초문법 - 52] Optional Protocol (0) | 2024.08.19 |
[Swift 기초문법 - 50] 자료형 체크 (0) | 2024.08.19 |
[Swift 기초문법 - 49] 프로토콜 조건 적용 (1) | 2024.08.19 |
[Swift 기초문법 - 48] Toggle (0) | 2024.08.19 |