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语句必须穷举所有情况,否则无法通过编译。 关联值可以为枚举的成员值设置一个常量或者变量。每个枚举成员的关联值类型可以不同。 商品可以用条形码或者二维码来标识。使用0到9的数字的UPC格式的一维条形码来表示,第一位表示“数字系统”,后面5位表示“厂商代码”,再后面5位表示“产品代码”,最后是“检查”位。 8 85909 51226 3 在 Swift 中,使用如下方式定义表示两种商品条形码的枚举: enum Barcode {
case upc(Int,Int,Int)//条形码
case qrCode(String)//二维码
}
定义一个名为 Barcode 的枚举类型,它的一个成员值是具有 (Int,Int,Int,Int) 类型关联值的 upc ,另一个 这个定义并没有提供确切的Int或者String值,它只是定义了当常量或变量是 Barcode.upc 或者Barcode.qrCode 时,存储的关联值类型。 var productBarcode = Barcode.upc(8,85909,51226,3)
上面创建了一个名为productBarcode 的变量,并将Barcode.upc赋值给它,关联的元组值为(8,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),(check).")
case let .qrCode(productCode):
print("QR code: (productCode).")
}
raw values 原始值可以为枚举成员设置默认值,也就是原始值,并且原始值的类型必须相同。 enum ASCIIControlCharacter: Character {
case tab = "t"
case lineFeed = "n"
case carriageReturn = "r"
}
原始值的隐式赋值原始值的隐式赋值:原始值为整数或者字符串类型的枚举,swift会自动赋值。 rawValue 属性 enum Planet: Int {
case mercury = 1,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"
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |