枚舉 蘋果官方文檔 "枚舉" 蘋果官方文檔中文翻譯 "枚舉" 枚舉語法 使用Switch語句來匹配枚舉值 如果不能為所有枚舉成員都提供一個 case,那你也可以提供一個==default==情況來包含那些不能被明確寫出的成員: 關聯值 如果對於一個枚舉成員的所有的相關值都被提取為常量,或如果都被提取 ...
枚舉
蘋果官方文檔 枚舉
蘋果官方文檔中文翻譯 枚舉
枚舉語法
enum SomeEnumeration {
// enumeration definition goes here
}
enum CompassPoint {
case north
case south
case east
case west
}
var directionToHead = CompassPoint.west
directionToHead = .east
enum Planet {
case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}
使用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"
如果不能為所有枚舉成員都提供一個 case,那你也可以提供一個==default==情況來包含那些不能被明確寫出的成員:
let somePlanet = Planet.earth
switch somePlanet {
case.earth:
print("Mostly harmless")
default:
print("Not a safe place for humans")
}
// Prints "Mostly harmless"
關聯值
enum Barcode {
case upc(Int, Int, Int, Int)
case qrCode(String)
}
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
productBarcode = .qrCode("ABCDEFGHIJKLMNOP")
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).")
}
// Prints "QR code: ABCDEFGHIJKLMNOP."
如果對於一個枚舉成員的所有的相關值都被提取為常量,或如果都被提取為變數,為了簡潔,你可以用一個單獨的 var或 let在成員名稱前標註:
switch productBarcode {
case let .upc(numberSystem, manufacturer, product, check):
print("UPC : \(numberSystem), \(manufacturer), \(product), \(check).")
case let .qrCode(productCode):
print("QR code: \(productCode).")
}
// Prints "QR code: ABCDEFGHIJKLMNOP."
原始值
枚舉成員可以用相同類型的預設值預先填充(稱為原始值)。
enum ASCIIControlCharacter: Character {
case tab = "\t"
case lineFeed = "\n"
case carriageReturn = "\r"
}
隱式指定的原始值
當你在操作存儲整數或字元串原始值枚舉的時候,你不必顯式地給每一個成員都分配一個原始值。當你沒有分配時,Swift 將會自動為你分配值。
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"
從原始值初始化
用原始值類型來定義一個枚舉,那麼枚舉就會自動收到一個可以接受原始值類型的值的初始化器(叫做==rawValue==的形式參數)然後返回一個枚舉成員或者 nil
let possiblePlanet = Planet(rawValue: 7)
// possiblePlanet is of type Planet? and equals Planet.Uranus
原始值初始化器是一個可失敗初始化器
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"
遞歸枚舉
遞歸枚舉是擁有另一個枚舉作為枚舉成員關聯值的枚舉。在聲明枚舉成員之前使用==indirect==關鍵字來明確它是遞歸的。
enum ArithmeticExpression {
case number(Int)
indirect case addition(ArithmeticExpression, ArithmeticExpression)
indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}
可以在枚舉之前寫==indirect==來讓整個枚舉成員在需要時可以遞歸:
indirect enum ArithmeticExpression {
case number(Int)
case addition(ArithmeticExpression, ArithmeticExpression)
case multiplication(ArithmeticExpression, ArithmeticExpression)
}
let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let sum = ArithmeticExpression.addition(five, four)
let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(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)
}
}
print(evaluate(product))
// Prints "18"