The Swift Programming Language - (8)- 枚举

枚举

枚举定义与在OC中类似,不同的是swift中的枚举更加灵活。

重点: 枚举的关联值与原始值 和 递归枚举

枚举语法

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

与 C 和 Objective-C 不同,Swift枚举成员在被创建时没有默认值,就是说north、south、east、west 不会被隐式地赋值为0、1、2、3.

多个case可以写在一行,用逗号隔开

enum Planet {
    case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}

枚举命名提倡开头字母大写。给枚举值使用单数而不是复数,这样方便理解使用。

var directionToHead = CompassPoint.west

directionToHead 在初始化时会被推断出类型,当要更改directionToHead的值时,可以用更简单的点语法

directionToHead = .east

因为已经知道了directionToHead 的类型,所以可以用点语法设置它的值。显式类型的枚举值能提高可读性。

使用Switch语句匹配枚举值

可以在switch中使用枚举值判断

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"

**强制穷举:在判断一个枚举类型的值时,switch语句必须穷举所有情况,否则无法通过编译。
当不需要匹配每个枚举成员的时候,你可以提供一个default分支来涵盖所有未明确处理的枚举成员。**

关联值

可以为枚举的成员值设置一个常量或者变量。每个枚举成员的关联值类型可以不同。

商品可以用条形码或者二维码来标识。使用0到9的数字的UPC格式的一维条形码来表示,第一位表示“数字系统”,后面5位表示“厂商代码”,再后面5位表示“产品代码”,最后是“检查”位。

8 85909 51226 3
另一种是QR码格式的二维码。

在 Swift 中,使用如下方式定义表示两种商品条形码的枚举:

enum Barcode {
    case upc(Int, Int, Int, Int)//条形码
    case qrCode(String)//二维码
}

定义一个名为 Barcode 的枚举类型,它的一个成员值是具有 (Int,Int,Int,Int) 类型关联值的 upc ,另一个
成员值是具有 String 类型关联值的 qrCode 。

这个定义并没有提供确切的Int或者String值,它只是定义了当常量或变量是 Barcode.upc 或者Barcode.qrCode 时,存储的关联值类型。

var productBarcode = Barcode.upc(8, 85909, 51226, 3) 

上面创建了一个名为productBarcode 的变量,并将Barcode.upc赋值给它,关联的元组值为(8, 85909, 51226, 3) 。

使用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).")
}

如果一个枚举成员的所有关联值都被提取为常量,或者变量,为了简洁,你可以只在成员名称前标注一个let或var:

switch productBarcode {
case let .upc(numberSystem, manufacturer, product, check):
    print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
case let .qrCode(productCode):
    print("QR code: \(productCode).")
}

raw values 原始值

可以为枚举成员设置默认值,也就是原始值,并且原始值的类型必须相同。
原始值可以是字符串、字符、任意整型值或浮点型值。每个原始值在枚举声明中必须是唯一的。

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

原始值与关联值: 原始值是在定义枚举时被预先填充的值,始终不变;关联值是创建一个基于枚举成员的常量或者变量时才设置的值,可以变化。

原始值的隐式赋值

原始值的隐式赋值:原始值为整数或者字符串类型的枚举,swift会自动赋值。
当使用整数作为原始值时,隐式赋值的值依次递增1.若第一个枚举成员没有设置原始值,其原始值将为0.
当使用字符串作为枚举类型的原始值时,每个枚举成员的隐式原始值为该枚举成员的名称。

rawValue 属性

enum Planet: Int {
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}
let earthsOrder = Planet.earth.rawValue // earthsOrder 值为 3

enum CompassPoint: String {
    case north, south, east, west
}
let sunsetDirection = CompassPoint.west.rawValue // sunsetDirection 值为 "west"

使用原始值初始化枚举实例

这个例子利用原始值 7 创建了枚举成员 uranus :

let possiblePlanet = Planet(rawValue: 7)
// possiblePlanet 类型为 Planet? 值为 Planet.uranus

原始值构造器是一个可失败构造器,因为并不是每一个原始值都有与之对应的枚举成员。

递归枚举

递归枚举是一种枚举类型,它有一个或者多个枚举成员使用该枚举类型的实例作为关联值。在枚举成员前加上indirect来表示该成员可递归。在枚举类型前加上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)) // 打印 "18"

猜你喜欢

转载自blog.csdn.net/liyun123gx/article/details/73604514