溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用swift枚舉定義

發(fā)布時間:2021-10-14 10:08:57 來源:億速云 閱讀:109 作者:iii 欄目:編程語言

本篇內(nèi)容主要講解“如何使用swift枚舉定義”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“如何使用swift枚舉定義”吧!

枚舉定義

//枚舉定義
enum CompassPoint {
    case north
    case south
    case east
    case west
}

//常配合switch case使用
let directionToHead = CompassPoint.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")
}
/*
 Watch out for penguins
 */

枚舉遍歷

//枚舉遍歷
enum Planet: CaseIterable {
    case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}

for planet in Planet.allCases {
    print(planet)
}
/*
 mercury
 venus
 earth
 mars
 jupiter
 saturn
 uranus
 neptune
 */

關(guān)聯(lián)值

//關(guān)聯(lián)值
enum Barcode {
    case upc(Int, Int, Int, Int)
    case qrCode(String)
}

func showBarcode(_ barcode: Barcode) {
    switch barcode {
    case .upc(let numberSystem, let manufacturer, let product, let check):
        print("UPC:\(numberSystem), \(manufacturer), \(product), \(check)")
    case .qrCode(let productCode):
        print("QR Code: \(productCode)")
    }
}

var productBarcode = Barcode.upc(1, 1, 1, 1)
showBarcode(productBarcode)
//UPC:1, 1, 1, 1
productBarcode = Barcode.qrCode("hello")
showBarcode(productBarcode)
//QR Code: hello

原始值

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

從原始值初始化

//從原始值初始化
enum RoleStatus: Int,CaseIterable {
    case run
    case jump
    case walk
    case idle
}

for i in 0...RoleStatus.allCases.count {
    print(RoleStatus(rawValue: i))
}
/*
 Optional(__lldb_expr_8.RoleStatus.run)
 Optional(__lldb_expr_8.RoleStatus.jump)
 Optional(__lldb_expr_8.RoleStatus.walk)
 Optional(__lldb_expr_8.RoleStatus.idle)
 nil
 */

遞歸枚舉(indirect)

//遞歸枚舉 (5+4)*2
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))
//18

到此,相信大家對“如何使用swift枚舉定義”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI