Enumerations 程式範例— Apple’s The Swift Programming Language
Enumeration Syntax
寫法1: 一個 case 一行。
enum CompassPoint { case north case south case east case west}
寫法2: 寫在同一行,逗號分隔。
enum Planet { case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune}
設定 enum 型別變數的內容。
var directionToHead = CompassPoint.westdirectionToHead = .east
Matching Enumeration Values with a Switch Statement
比較 enum 的每個 case。
directionToHead = .southswitch 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")}
搭配 default。
let somePlanet = Planet.earthswitch somePlanet {case .earth: print("Mostly harmless")default: print("Not a safe place for humans")}
Iterating over Enumeration Cases
遵從 CaseIterable protocol,利用 allCases 取得 enum 所有 case 的 array。
enum Beverage: CaseIterable { case coffee, tea, juice}let numberOfChoices = Beverage.allCases.countprint("\(numberOfChoices) beverages available")
讀取 enum 的每個 case。
for beverage in Beverage.allCases { print(beverage)}
Associated Values
在 enum case 裡儲存資料,可搭配任何型別。
Barcode 例子:


enum Barcode { case upc(Int, Int, Int, Int) case qrCode(String)}
產生 upc 和 qrCode。
var productBarcode = Barcode.upc(8, 85909, 51226, 3)productBarcode = .qrCode("ABCDEFGHIJKLMNOP")
利用 switch 比較 enum Associated Values 的內容
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).")}
將 let 移到前面,變成 case let。
switch productBarcode {case let .upc(numberSystem, manufacturer, product, check): print("UPC : \(numberSystem), \(manufacturer), \(product), \(check).")case let .qrCode(productCode): print("QR code: \(productCode).")}
Raw Values
每個 case 搭配同一型別的預設值,型別只能是 String,Character 或是整數,浮點數相關型別。
enum ASCIIControlCharacter: Character { case tab = "\t" case lineFeed = "\n" case carriageReturn = "\r"}
Implicitly Assigned Raw Values
預設產生的 raw value。
Int 會依序遞增。
enum Planet: Int { case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune}
產生和 case 名字一樣的 raw value 字串。
enum CompassPoint: String { case north, south, east, west}
從 property rawValue 讀取 raw value。
let earthsOrder = Planet.earth.rawValuelet sunsetDirection = CompassPoint.west.rawValue
Initializing from a Raw Value
從 raw value 生成 enum 資料。
let possiblePlanet = Planet(rawValue: 7)從 raw value 生成的 enum 資料是 optional,有可能 nil。
let positionToFind = 11if 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)")}
Recursive Enumerations
寫法1: case 加 indirect
enum ArithmeticExpression { case number(Int) indirect case addition(ArithmeticExpression, ArithmeticExpression) indirect case multiplication(ArithmeticExpression, ArithmeticExpression)}
寫法2: enum 加 indirect
indirect enum ArithmeticExpression { case number(Int) case addition(ArithmeticExpression, ArithmeticExpression) case multiplication(ArithmeticExpression, ArithmeticExpression)}
應用: 計算 (5 + 4) * 2
func evaluate(_ expression: ArithmeticExpression) -> Int { switch expression { case let .number(value): return value case let .addition(left, right): return evaluate(left) + evaluate(right) case let .multiplication(left, right): return evaluate(left) * evaluate(right) }}let five = ArithmeticExpression.number(5)let four = ArithmeticExpression.number(4)let sum = ArithmeticExpression.addition(five, four)let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))print(evaluate(product))
