스위프트 Enum

Swift Terminology - Enumeration

Sources from The Swift Programming Language (Swift 4), Appventure.me

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.

필요한 상황

기본 문법

enum CompassPoint {
    case north
    case south
    case east
    case west
}

스위치문과 함께 사용하기

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"

값 할당하기 (Associated Values)

01. 값을 할당하는 “바코드” 열거형 만들기

enum Barcode {
    case upc(Int, Int, Int, Int) // 이건 어찌보면 튜플이 할당된 케이스!
    case qrCode(String)
}

02. 바코드의 인스턴스 만들기

var productBarcode: Barcode = Barcode.upc(1, 2, 3, 4)
productBarcode = .qrCode("BONJOUR")

03. 스위치문 사용하기

👋🏻 스위프트 구문 안에서 값 바인딩 시키기
switch productBarcode {
    case .upc(let numberSystem, let manufacturer, let product, let check):
    print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
    case .qrCode(let productCode):
    print("QR CODE: \(productCode).")
}
👋🏻 와일드카드를 사용할 수도 있다.
switch bookBarcode {
    case .upc(let numberSystem, _, _, _) where numberSystem == 8:
        print("This is imported Journals.")
    case .qrCode(let productCode):
        print("QR CODE: \(productCode).")
	default:
    print("default")
}

04. 예제: 바코드 첫번째 숫자로 책 타입을 구분해보기

enum Barcode {
    case upc(Int, Int, Int, Int)
    case qrCode(String)
}

var kinfolk: Barcode = Barcode.upc(8, 2, 3, 4)
var photography: Barcode = Barcode.upc(111, 2, 3, 4)
var foody: Barcode = Barcode.upc(11, 2, 3, 4)

func checkBookType(bar: Barcode) {
    switch bar {
    case .upc(let bookType, _, _, _) where bookType < 10 && 0 < bookType:
        print("This is a journal.")
    case .upc(let bookType, _, _, _) where bookType >= 10 && 20 > bookType:
        print("This is a cook Book.")
    case .upc(let bookType, _, _, _) where bookType >= 20 && 500 > bookType:
        print("This is a photography book.")
    default:
        print("Anything else")
    }
}

checkBookType(bar: kinfolk)
checkBookType(bar: photography)
checkBookType(bar: foody)

Enum Values: Raw Value

Mapping to String

enum House: String {
    case Baratheon = "Ours is the Fury"
    case Greyjoy = "We Do Not Sow"
    case Martell = "Unbowed, Unbent, Unbroken"
    case Stark = "Winter is Coming"
    case Tully = "Family, Duty, Honor"
    case Tyrell = "Growing Strong"
}

Mapping to Integer, 디폴트는 0부터 ~

enum Planet: Int {
    case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
} // mercury = 0

let possiblePlanet:Planet = Planet(rawValue: 1)! // venus
print(possiblePlanet.rawValue) // 1

Mapping to Integer, 시작값 지정 가능

enum Planet: Int {
    case mercury=3, venus, earth, mars, jupiter, saturn, uranus, neptune
} // mercury = 3

let possiblePlanet:Planet = Planet(rawValue: 1)! // venus
print(possiblePlanet.rawValue) // 1

Mapping to floating point

enum Constants: Double {
    case π = 3.14159
    case e = 2.71828
    case φ = 1.61803398874
    case λ = 1.30357
}

Properties

enum Device {
  case iPad, iPhone
  var year: Int {
    switch self {
	case iPhone: return 2007 // enum의 케이스 하위에 year 생성됨
	case iPad: return 2010
     }
  }
}