Swift枚舉

枚舉簡單認(rèn)識(shí)

  • Swift中的枚舉比OC中的枚舉強(qiáng)大, 因?yàn)镾wift中的枚舉是一等類型, 它可以像類和結(jié)構(gòu)體一樣增加屬性和方法。

語法

enum 枚舉名{
    case
    case
    case
    ...
}

舉例:

//多個(gè)case分開寫
enum CompassPoint {
    case north
    case south
    case east
    case west
}

//多個(gè)case寫在同一行,使用逗號(hào)分開
enum Planet {
    case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}

//創(chuàng)建CompassPoint的枚舉實(shí)例
var directionToHead = CompassPoint.west
directionToHead = .east

//也可以寫成
//var directionToHead:CompassPoint = .west

注意:與C和Objective-C不同,Swift枚舉在創(chuàng)建時(shí)默認(rèn)不分配整數(shù)值。在CompassPoint例子中,未指明CompassPoint成員的類型north,south,east和west不等于隱式0,1,2和3。
如果給north指定原始整數(shù)值,后面的值默認(rèn)+1:

enum CompassPoint {
     case north = 5, south, east, west
}

枚舉與Switch結(jié)合

  • 通常將單個(gè)枚舉值與switch語句匹配
//switch覆蓋了CompassPoint的所有情況,則可省略default
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")
}

//switch沒有覆蓋Planet的所有情況,需要添加default
let somePlanet = Planet.earth
switch somePlanet {
case .earth:
    print("Mostly harmless")
default:
    print("Not a safe place for humans")
}

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

定義Swift枚舉來存儲(chǔ)任何給定類型的關(guān)聯(lián)值,而且每種枚舉情況的值類型可以不同

enum Barcode {
    case upc(Int, Int, Int, Int)
    case qrCode(String)
}
//創(chuàng)建Barcode枚舉實(shí)例
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
productBarcode = .qrCode("ABCDEFGHIJKLMNOP")

可以將關(guān)聯(lián)值提取為switch語句的一部分。將每個(gè)關(guān)聯(lián)值提取為常量(let)或變量(var),以便在switch中處理:

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

如果枚舉case的所有關(guān)聯(lián)值都被提取為常量,或者都被提取為變量,則可以將var或let放置在case名稱前面,來提取所有的關(guān)聯(lián)值:

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

原始值(Raw Values)

  • 隱式分配原始值
    當(dāng)使用存儲(chǔ)整數(shù)或字符串原始值的枚舉時(shí),不必為每種case顯式分配原始值,Swift將自動(dòng)為其分配值。如果使用整數(shù)為原始值,則每個(gè)case的原始值依次自增1。若第一個(gè)case沒有設(shè)置原始值,則默認(rèn)為0:
enum Planet: Int {    //【Int表示原始值類型】
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}

當(dāng)使用字符串作為原始值類型時(shí),每個(gè)case的原始值是該case的名稱。

enum CompassPoint: String {
    case north, south, east, west
}

發(fā)現(xiàn)去掉String原始值類型,每個(gè)case的原始值也是該case的名稱

  • 使用rawValue將枚舉值轉(zhuǎn)換為原始值:
let earthsOrder = Planet.earth.rawValue
// earthsOrder is 3
let sunsetDirection = CompassPoint.west.rawValue
// sunsetDirection is "west"
  • 原始值初始化枚舉實(shí)例:
let directionToHead = CompassPoint(rawValue: "north")!

注意點(diǎn):1.原始值區(qū)分大小寫。2.返回的是一個(gè)可選值,因?yàn)樵贾祵?yīng)的枚舉值不一定存在。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容