반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- github
- dictionary
- autolayout
- 문자열
- Swift
- string
- LazyHStack
- remote config
- ios
- Alamofire
- WebView
- RxSwift
- UIButton
- UITabBarController
- NavigationLink
- SwiftLint
- SwiftUI
- Realtime Database
- swipe
- 라이트모드
- 다크모드
- Firebase
- UIScrollView
- Apple
- 웹뷰
- Android
- gcd
- Java
- Observable
- subscript
Archives
- Today
- Total
점진적 과부하 개발 블로그
Swift 열거형(enum) 본문
반응형
열거형(enum)
- 관련있는 데이터들이 멤버로 구성되어 있는 자료형 객체
- 원치 않는 값이 잘못 입력되는 것을 방지
- 특정 값 중 하나만 선택하게 할 때
- 예) 성별, 색깔 등 같은 값들
enum Color { // 열거형명
case Red, Green, Blue // 열거형 정의, 멤버
} // 하나의 case문의 멤버들 나열하는 것도 가능
enum Language {
case Korean
case English
case French
case Japanese
}
print(Language.Korean) // Korean
var x = Language.Japanese // Japnese
x = .French // French
// 문맥에서 타입의 추론이 가능한 시점(등호 좌변의 변수 타입이 확정적일 때)에는 열거형명 생략 가능
enum Language {
case Korean
case English
case French
case Japanese
}
var choice : Language
choice = .Korean
switch choice { // switch의 비교값이 열거형 Language
case .Korean: // choice .Korean이면 "한국어" 출력
print("한국어")
case .English:
print("영어")
case .French:
print("불어")
case .Japanese:
print("일본어") // 모든 열거형 case를 포함하면 default가 없어도 된다.
}
Swift 열거형 멤버에는 메서드도 가능
enum gender: String {
case male,Female
func xx() {
switch self {
case .male:
print("남자")
case .Female:
print("여자")
}
}
}
gender.Female.xx() // 여자
열거형의 rawValue
enum Num: Int {
case one // 0
case two = 3
case three // 4
}
print(Num.one) // one
print(Num.three) // three
print(Num.one.rawValue) // 0
print(Num.three.rawValue) // 4
enum Week: String {
case Monday = "월"
case Tuseday = "화"
case Wednesday = "수"
case Thursday = "목"
case Friday = "금"
case Saturday // 값이 지정되지 않으면 case 이름이 할당된다.
case Sunday
}
print(Week.Monday) // Monday
print(Week.Monday.rawValue) // 월
print(Week.Sunday) // Sunday
print(Week.Sunday.rawValue) // Sunday
연관 값(associated value)을 갖는 Enum
enum Date {
case intDate(Int,Int,Int) //(Int,Int,Int)형을 연관값을 갖는 intDate
case stringDate(String) //String형 연관값을 갖는 stringDate
}
var todayDate = Date.intDate(2021, 12, 23)
todayDate = Date.stringDate("2021년 12월 25일") // 주석처리하면 "2021년 12월 23일" 출력
switch todayDate {
case .intDate(let year, let month, let day):
print("\(year)년 \(month)월 \(day)일")
case .stringDate(let date):
print(date)
}
옵셔널은 연관 값(associated value)을 갖는 enum
let age: Int? = 30 //optional(30)
switch age {
case .none: // nil인 경우
print("나이 정보가 없습니다.")
case .some(let a) where a < 20:
print("\(a)살 미성년자입니다")
case .some(let a) where a < 71:
print("\(a)살 성인입니다.")
default:
print("경로우대입니다.")
} // 30살 성인입니다.
var x : Int? = 20
var y : Int? = Optional.some(10)
var z : Int? = Optional.none
var d : Optional<Int> = 30
print(x,y,z,d) //Optional(20), Optional(10), nil, Optional(30)
참고 문헌 : iOS 프로그래밍 실무 한성현 교수님 강의 내용 변형 및 요약
반응형
'Swift > 문법' 카테고리의 다른 글
Swift 클로저(Closure) (0) | 2022.01.06 |
---|---|
Swift guard문 (0) | 2022.01.05 |
Swift 문법 정리 1 (0) | 2022.01.03 |
Swift 클래스와 구조체 (0) | 2021.12.26 |
Swift 구조체(Struct) (0) | 2021.12.23 |