Swift 3.0基礎(chǔ)學(xué)習(xí)之枚舉類型
枚舉語(yǔ)法
使用關(guān)鍵字 enum 定義一個(gè)枚舉
enum SomeEnumeration { // enumeration definition goes here }
例如,指南針有四個(gè)方向:
enum CompassPoint { case north case south case east case west }
這里跟 c 和 objective-c 不一樣的是,Swift 的枚舉成員在創(chuàng)建的時(shí)候沒有給予默認(rèn)的整型值。所以上面代碼中的東南西北并不是0到3,相反,不同的枚舉類型本身就是完全成熟的值,具有明確定義的CompassPoint類型。
也可以聲明在同一行中:
enum Planet { case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune }
枚舉賦值:
var directionToHead = CompassPoint.west
一旦 directionToHead 明確為 CompassPoint 類型的變量,后面就可以使用點(diǎn)語(yǔ)法賦值:
directionToHead = .east
Switch 表達(dá)式的枚舉值匹配
switch 表達(dá)式如下:
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"
當(dāng)然這里也可以加上 default 以滿足所有的情況:
let somePlanet = Planet.earth switch somePlanet { case .earth: print("Mostly harmless") default: print("Not a safe place for humans") } // Prints "Mostly harmless"
關(guān)聯(lián)值
在 Swift 中,使用枚舉來(lái)定義一個(gè)產(chǎn)品條形碼:
enum Barcode { case upc(Int, Int, Int, Int) case qrCode(String) }
可以這樣理解上面這段代碼:定義一個(gè)叫 Barcode 的枚舉類型,帶有值類型(Int,Int,Int,Int)的 upc和值類型(String)
現(xiàn)在可以這樣創(chuàng)建其中一種類型的條形碼:
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
同一產(chǎn)品的另外一個(gè)類型的條形碼可以這樣賦值:
productBarcode = .qrCode("ABCDEFGHIJKLMNOP")
可以用 switch 來(lái)查看兩種不同的條形碼類型:
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."
上面的寫法也可以改為:
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."
原始值
這里是一個(gè)保存原始 ASCII 值的枚舉類型:
enum ASCIIControlCharacter: Character { case tab = "\t" case lineFeed = "\n" case carriageReturn = "\r" }
和上面關(guān)聯(lián)值類似,在枚舉中也可以指定每個(gè) case 的默認(rèn)值(raw values)。
值得注意的是,原始值和關(guān)聯(lián)值不一樣,原始值是在第一次定義枚舉代碼的時(shí)候已經(jīng)設(shè)置好的,所有同類型的枚舉 case 的原始值都是一樣的。而關(guān)聯(lián)值是你創(chuàng)建一個(gè)基于枚舉 case新的常量或者變量的時(shí)候設(shè)置的,可以在每次你創(chuàng)建的時(shí)候都使用不一樣的值。
隱式分配的原始值
如果枚舉中 case 的原始值是整型或者字符串的時(shí)候,你不需要給每個(gè) case 分配原始值,Swift 會(huì)自動(dòng)幫你分配好值。
例如:
enum Planet: Int { case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune }
Planet.mercury 原始值為1,Planet.venus 擁有一個(gè)隱式的原始值為2,以此類推。
如果枚舉的原始值是 string 類型,那么他的原始值就是 case 名稱的文本,例如:
enum CompassPoint: String { case north, south, east, west } CompassPoint.south 的隱式原始值是"south", 以此類推。 let earthsOrder = Planet.earth.rawValue // earthsOrder is 3 let sunsetDirection = CompassPoint.west.rawValue // sunsetDirection is "west"
從原始值初始化
如果你定義一個(gè)原始值類型的枚舉,這時(shí)枚舉會(huì)自動(dòng)創(chuàng)建一個(gè)帶有原始值類型的初始化器(參數(shù)名稱為 rawValue),例如:
let possiblePlanet = Planet(rawValue: 7) // possiblePlanet is of type Planet? and equals Planet.uranus
不是所有的 Int 值都可以找到對(duì)應(yīng)的 planet,所以原始值初始化器會(huì)返回一個(gè) optional 的枚舉 case,上面的例子中的 possiblePlanet 是 Planet? 類型。
如果你想找原始值為11的 planet,初始化器將返回 nil:
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"
遞歸枚舉
遞歸枚舉是一個(gè)包含有一個(gè)或多個(gè)枚舉 case 的關(guān)聯(lián)值枚舉實(shí)例的枚舉,使用關(guān)鍵字 indirect 標(biāo)明某個(gè)枚舉 case 是遞歸的。
例如,下面是一個(gè)保存簡(jiǎn)單算法表達(dá)式的枚舉:
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) }
下面的代碼是示例如何創(chuàng)建這個(gè)遞歸枚舉:
let five = ArithmeticExpression.number(5) let four = ArithmeticExpression.number(4) let sum = ArithmeticExpression.addition(five, four) let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))
應(yīng)用到計(jì)算函數(shù)中:
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"
上面例子的算法表達(dá)式是:(5 + 4) * 2,結(jié)果為18
參考英語(yǔ)原文:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-ID145
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者使用Swift能帶來(lái)一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Swift map和filter函數(shù)原型基礎(chǔ)示例
這篇文章主要為大家介紹了Swift map和filter函數(shù)原型基礎(chǔ)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07深入解析Swift中switch語(yǔ)句對(duì)case的數(shù)據(jù)類型匹配的支持
這篇文章主要介紹了Swift中switch語(yǔ)句對(duì)case的數(shù)據(jù)類型匹配的支持,Swift中switch...case語(yǔ)句支持多種數(shù)據(jù)類型的匹配判斷,十分強(qiáng)大,需要的朋友可以參考下2016-04-04詳談swift內(nèi)存管理中的引用計(jì)數(shù)
下面小編就為大家?guī)?lái)一篇詳談swift內(nèi)存管理中的引用計(jì)數(shù)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-09-09Swift利用指紋識(shí)別或面部識(shí)別為應(yīng)用添加私密保護(hù)功能
這篇文章主要給大家介紹了關(guān)于Swift利用指紋識(shí)別或面部識(shí)別為應(yīng)用添加私密保護(hù)功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用swift具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面隨著小編來(lái)一起看看吧2018-05-05