Swift 的 列舉(Enum) 教學

在程式設計上,相信大家對於列舉 (enumeration) 都很熟悉,列舉其實就是定義可能的情況,在發生意外情況時,程式就不會執行,算是一種相當安全的程式設計。當然,在 Swift 中一定要有列舉這個功能,但 Swift 的列舉更強了,接著讓我們看一下 Swift 的 enumeration 是怎麼操作的吧!

在 Swift 中,我們一樣是以 enum 關鍵字來做開始,而每個情況都要使用 case 來定義,並且要換行。

enum CompassPoint {
    case north
    case south
    case east
    case west
}

Swift 強調精簡,允許一個 case 後面有多個定義,所以也可以寫得更簡單。

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

要設定列舉值至參數,只要使用「.列舉值」就可以了。要判斷是符合那一種條件,最簡單的就是使用 switch-case 的方式囉。

var 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 將「 條件1 , 條件2,… 」 串接起來就可以了。

var directionToHead = .east
switch directionToHead {
case .north:
    print("Lots of planets have a north")
case .south, .east:
    print("Watch out for penguins or Where the sun rises")
case .west:
    print("Where the skies are blue")
}
// Prints "Watch out for penguins or Where the sun rises"

但是,有時我們會希望列舉的值是整數或是字串,這時我們該怎麼做呢?只要加上 「: 資料型別」就可以囉,而資料別型限定字串、字元、整數、浮點數這四種,自己寫的資料型別並不支援喔!底下是一個範例。

enum Planet: Int {
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}

如果要將整數設定到對應列舉值,也很簡單,只要用 rawValue 就可以了。

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

想當然,如果要取出值,也是直接用 rawValue 就好。

let earthsOrder = Planet.earth.rawValue
// earthsOrder is 3

let sunsetDirection = CompassPoint.west.rawValue
// sunsetDirection is "west"

底下是另一個範例,當我們設的值不在列舉的範圍中,就會回傳 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"

列舉也可以使用相當複雜的型別,如我們掃碼可能有 QRCode 或是 BarCode,我們就可以使用列舉定義 associated value。

enum Barcode {
    case upc(Int, Int, Int, Int)
    case qrCode(String)
}

接下來,我們以下的方式來設定產品的 QRCode 或是 BarCode,並且一樣用 switch-case 的方式做判斷。

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

甚至可以更精簡的拿掉 let 喔!

var productBarcode = Barcode.upc(8, 85909, 51226, 3)
productBarcode = .qrCode("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."

還有一個問題是,我們要取出我們總共有幾個列舉值,也很簡單,只要實做 CaseIterable 這個 Portocol 就可以了,而要取出共有幾個,則是使用「列舉名.allCases.count」。

enum Beverage: CaseIterable {
    case coffee, tea, juice
}
let numberOfChoices = Beverage.allCases.count
print("\(numberOfChoices) beverages available")
// Prints "3 beverages available"

要印出這裡面所有項目,也一樣要實做 CaseIterable 這個 Protocol喔,再使用 for-in 就可以了。

for beverage in Beverage.allCases {
    print(beverage)
}
// coffee
// tea
// juice

在 Swift 中,可以使用「遞迴列舉 (Recursive Enumerations)」,必須使用 indirect 關鍵字來做修飾。以下例來說,列舉 ArithmeticExpression 的條件 addition、multiplication 都包含 ArithmeticExpression 自己本身,所以我們要加上 indirect 這個關鍵字。

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

Swift 非常人性化,如果想把整個列舉的全部條件都變成可遞迴的,只要在 enum 前加上關鍵字 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"
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments