Swift4 枚舉,自學筆記

來源:互聯網
上載者:User

標籤:cat   隱式   失敗   type   possible   var   ctc   蘋果   自動   

枚舉
蘋果官方文檔 枚舉蘋果官方文檔中文翻譯 枚舉
枚舉文法
enum SomeEnumeration {    // enumeration definition goes here}
enum CompassPoint {    case north    case south    case east    case west}var directionToHead = CompassPoint.westdirectionToHead = .east
enum Planet {    case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune}
使用Switch語句來匹配枚舉值
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")}// prints "Watch out for penguins"

如果不能為所有枚舉成員都提供一個 case,那你也可以提供一個==default==情況來包含那些不能被明確寫出的成員:

let somePlanet = Planet.earthswitch 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 = 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)")}// 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"

Swift4 枚舉,自學筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.