석지한
한지석일지
석지한
전체 방문자
오늘
어제
  • 분류 전체보기 (33)
    • iOS & Swift (15)
    • 프로젝트 (13)
    • 알고리즘 (0)
    • 잡담 (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Swift
  • Python
  • TableView
  • 알고리즘
  • BFS
  • 동적프로그래밍
  • 이분탐색
  • 그리디
  • 개발일지
  • dp
  • 시간표
  • AppDelegate
  • 공식문서
  • 인터뷰
  • Greedy
  • dfs
  • ios
  • 프로젝트
  • Realm
  • pickerView

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
석지한

한지석일지

iOS & Swift

[Swift] Swift 공식문서 정리 - 4(열거형)

2022. 4. 6. 15:09
반응형

열거형이란 ?

- 같은 주제로 연관된 데이터들을 멤버로 구성하여 나타내는 자료형

 

열거형의 선언

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"

 

참고

https://babbab2.tistory.com/category/iOS/Swift

https://jusung.gitbook.io/the-swift-language-guide/

반응형

'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
    'iOS & Swift' 카테고리의 다른 글
    • [iOS] swift Alamofire 토큰 재발급 구현 방법(adapt, retry, RequestIntercepter)
    • [Swift] Swift 공식문서 정리 - 5(옵셔널 체이닝)
    • [Swift] Swift 공식문서 정리 - 3(클래스 & 구조체)
    • [Swift] Swift 공식문서 정리 - 2(제어문, 함수)
    석지한
    석지한
    iOS

    티스토리툴바