반응형
열거형이란 ?
- 같은 주제로 연관된 데이터들을 멤버로 구성하여 나타내는 자료형
열거형의 선언
enum CompassPoint {
case north
case south
case east
case west
}
var directionToHead = CompassPoint.west
// 이후엔 생략 가능
directionToHead = .east
Switch 구문에서 열거형의 사용
directionToHead = .south
switch directionToHead {
case .north:
print("Lots of planets have a north")
case .south:
print("Watch out for penguins")
case .east:
print("Where the sun rises")
case .west:
print("Where the skies are blue")
}
// Prints "Watch out for penguins"
RAW 값 사용
enum Planet: Int {
case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}
enum CompassPoint: String {
case north, south, east, west
}
let earthsOrder = Planet.earth.rawValue
// earthsOrder is 3
let sunsetDirection = CompassPoint.west.rawValue
// sunsetDirection is "west"
RAW값을 이용한 초기화
let positionToFind = 11
if let somePlanet = Planet(rawValue: positionToFind) {
switch somePlanet {
case .earth:
print("Mostly harmless")
default:
print("Not a safe place for humans")
}
} else {
print("There isn't a planet at position \(positionToFind)")
}
// Prints "There isn't a planet at position 11"
참고
반응형
'iOS & Swift' 카테고리의 다른 글
[iOS] swift Alamofire 토큰 재발급 구현 방법(adapt, retry, RequestIntercepter) (0) | 2022.04.25 |
---|---|
[Swift] Swift 공식문서 정리 - 5(옵셔널 체이닝) (0) | 2022.04.06 |
[Swift] Swift 공식문서 정리 - 3(클래스 & 구조체) (0) | 2022.04.04 |
[Swift] Swift 공식문서 정리 - 2(제어문, 함수) (0) | 2022.03.29 |
[Swift] Swift 공식문서 정리 - 1(기본연산자, 문자열, 콜렉션 타입) (0) | 2022.03.29 |