Swift4 枚舉,例子代碼

枚舉

蘋果官方文檔 枚舉

蘋果官方文檔中文翻譯 枚舉

枚舉語法

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 狀況來包含那些不能被明確寫出的成員:html

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在成員名稱前標註:express

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."

原始值

枚舉成員能夠用相同類型的默認值預先填充(稱爲原始值)。swift

enum ASCIIControlCharacter: Character {
    case tab = "\t"
    case lineFeed = "\n"
    case carriageReturn = "\r"
}

隱式指定的原始值

當你在操做存儲整數或字符串原始值枚舉的時候,你沒必要顯式地給每個成員都分配一個原始值。當你沒有分配時,Swift 將會自動爲你分配值。app

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的形式參數)而後返回一個枚舉成員或者 nilless

let possiblePlanet = Planet(rawValue: 7)
    // possiblePlanet is of type Planet? and equals Planet.Uranus

原始值初始化器是一個可失敗初始化器ui

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關鍵字來明確它是遞歸的。lua

enum ArithmeticExpression {
    case number(Int)
    indirect case addition(ArithmeticExpression, ArithmeticExpression)
    indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}

能夠在枚舉以前寫==indirect==來讓整個枚舉成員在須要時能夠遞歸:spa

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"
相關文章
相關標籤/搜索