@H_502_4@
import Foundation@H_502_4@
@H_502_4@
//在 Swift 中,枚举类型是一等公民(first-class)。它们采用了很多传统上只被类(class)所支持的特征,例如 计算型属性(computed properties),用于提供关于枚举当前值的附加信息,实例方法(instance methods),用于提供和枚举所代表的值相关联的功能。枚举也可以定义构造函数(initializers)来提供一个初始值;可以在原始的实现基础上扩展它们的功能;可以遵守协议(protocols)来提供标准的功能。@H_502_4@
@H_502_4@
@H_502_4@
/*枚举语法*********************************************************/@H_502_4@
//使用 enum 关键词来创建枚举并且把它们的整个定义放在一对大括号内:@H_502_4@
enum SomeEnumeration {@H_502_4@
// enumeration definition goes here@H_502_4@
}@H_502_4@
@H_502_4@
//以下是指南针四个方向的一个例子:@H_502_4@
enum CompassPoint {@H_502_4@
case North@H_502_4@
case South@H_502_4@
case East@H_502_4@
case West@H_502_4@
}@H_502_4@
@H_502_4@
//多个成员值可以出现在同一行上,用逗号隔开:@H_502_4@
enum Planet {@H_502_4@
case Mercury,Venus,Earth,Mars,Jupiter,Uranus,Neptune;@H_502_4@
}@H_502_4@
//每个枚举定义了一个全新的类型。像 Swift 中其他类型一样,它们的名字(例如 CompassPoint 和 Planet )必须以一个大写字母开头。给枚举类型起一个单数名字而不是复数名字,以便于读起来更加容易理解:@H_502_4@
var directionToHead = CompassPoint.West@H_502_4@
@H_502_4@
//directionToHead 的类型可以在它被 CompassPoint 的一个可能值初始化时推断出来。一旦 directionToHead 被声明为一个 CompassPoint,你可以使用一个缩写语法(.)将其设置为另一个 CompassPoint 的值:@H_502_4@
directionToHead = .East@H_502_4@
@H_502_4@
@H_502_4@
@H_502_4@
/*匹配枚举值和 Switch 语句*********************************************************/@H_502_4@
directionToHead = .South@H_502_4@
switch directionToHead {@H_502_4@
case .North:@H_502_4@
print("Lots of planets have a north")@H_502_4@
case .South:@H_502_4@
print("Watch out for penguins")@H_502_4@
case .East:@H_502_4@
print("Where the sun rises")@H_502_4@
case .West:@H_502_4@
print("Where the skies are blue")@H_502_4@
}@H_502_4@
@H_502_4@
//在判断一个枚举类型的值时,switch 语句必须穷举所有情况@H_502_4@
@H_502_4@
let somePlanet = Planet.Earth@H_502_4@
switch somePlanet {@H_502_4@
case .Earth:@H_502_4@
print("Mostly harmless")@H_502_4@
default:@H_502_4@
print("Not a safe place for humans")@H_502_4@
}@H_502_4@
// 输出 "Mostly harmless”@H_502_4@
@H_502_4@
@H_502_4@
@H_502_4@
/*相关值(Associated Values)*********************************************************/@H_502_4@
//在 Swift 中,使用如下方式定义两种商品条码的枚举:@H_502_4@
enum Barcode {@H_502_4@
case UPCA(Int,Int,Int)@H_502_4@
case QRCode(String)@H_502_4@
}@H_502_4@
var productBarcode = Barcode.UPCA(8,85909,51226,3)@H_502_4@
@H_502_4@
productBarcode = .QRCode("ABCDEFGHIJKLMNOP")@H_502_4@
@H_502_4@
switch productBarcode {@H_502_4@
case .UPCA(let numberSystem,let manufacturer,let product,let check):@H_502_4@
print("UPC-A: \(numberSystem),\(manufacturer),\(product),\(check).")@H_502_4@
case .QRCode(let productCode):@H_502_4@
print("QR code: \(productCode).")@H_502_4@
}@H_502_4@
@H_502_4@
//如果一个枚举成员的所有相关值被提取为常量,或者它们全部被提取为变量,为了简洁,你可以只放置一个var或者 let 标注在成员名称前:@H_502_4@
switch productBarcode {@H_502_4@
case let .UPCA(numberSystem,manufacturer,product,check):@H_502_4@
print("UPC-A: \(numberSystem),\(check).")@H_502_4@
case let .QRCode(productCode):@H_502_4@
print("QR code: \(productCode).")@H_502_4@
}@H_502_4@
@H_502_4@
@H_502_4@
@H_502_4@
/*原始值(Raw Values)*********************************************************/@H_502_4@
enum ASCIIControlCharacter: Character {@H_502_4@
case Tab = "\t"@H_502_4@
case CarriageReturn = "\r"@H_502_4@
}@H_502_4@
@H_502_4@
//原始值和相关值是不相同的。当你开始在你的代码中定义枚举的时候原始值是被预先填充的值,像上述三个 AS CII 码。对于一个特定的枚举成员,它的原始值始终是相同的。相关值是当你在创建一个基于枚举成员的新常量 或变量时才会被设置,并且每次当你这么做得时候,它的值可以是不同的。@H_502_4@
@H_502_4@
@H_502_4@
//下面的枚举是对之前 Planet 这个枚举的一个细化,利用原始整型值来表示每个 planet 在太阳系中的顺序:@H_502_4@
enum Planet2: Int {@H_502_4@
case Mercury = 1,Saturn,Neptune@H_502_4@
}@H_502_4@
//在上面的例子中,Plant.Mercury 赋了初值 1,Planet.Venus 会拥有隐式赋值 2,依次类推。@H_502_4@
@H_502_4@
//下面的例子是 CompassPoint 枚举类型的精简版,使用字符串作为初值类型,隐式初始化为每个方向的名称:@H_502_4@
enum CompassPoint2: String {@H_502_4@
case North,South,East,West@H_502_4@
}@H_502_4@
//上面例子中,CompassPoint.South 拥有隐式初值 South,依次类推。@H_502_4@
@H_502_4@
//使用枚举成员的 rawValue 属性可以访问该枚举成员的原始值:@H_502_4@
let earthsOrder = Planet2.Earth.rawValue // earthsOrder 值为 3@H_502_4@
print("earthsOrder is \(earthsOrder)")@H_502_4@
@H_502_4@
let sunsetDirection = CompassPoint2.West.rawValue// sunsetDirection 值为 "West"@H_502_4@
print("sunsetDirection is \(sunsetDirection)")@H_502_4@
@H_502_4@
//如果在定义枚举类型的时候使用了原始值,那么将会自动获得一个初始化方法,这个方法将原始值类型作为参 数,返回枚举成员或者nil 。你可以使用这种初始化方法来创建一个新的枚举变量。@H_502_4@
let possiblePlanet = Planet2(rawValue: 7)@H_502_4@
// possiblePlanet 类型为 Planet? 值为 Planet.Uranus@H_502_4@
//原始值构造器是一个可失败构造器,因为并不是每一个原始值都有与之对应的枚举成员@H_502_4@
@H_502_4@
@H_502_4@
//如果你试图寻找一个位置为9的行星,通过参数为rawValue构造函数返回的可选Planet值将是nil@H_502_4@
let positionToFind = 9@H_502_4@
if let somePlanet = Planet2(rawValue: positionToFind) {@H_502_4@
switch somePlanet {@H_502_4@
case .Earth:@H_502_4@
print("Mostly harmless")@H_502_4@
default:@H_502_4@
print("Not a safe place for humans")@H_502_4@
}@H_502_4@
} else {@H_502_4@
print("There isn't a planet at position \(positionToFind)")@H_502_4@
}@H_502_4@
// 输出 "There isn't a planet at position 9@H_502_4@
@H_502_4@
@H_502_4@
@H_502_4@
/*递归枚举(Recursive Enumerations)*********************************************************/@H_502_4@
@H_502_4@
//递归枚举(recursive enumeration) 是一种枚举类型,表示它的枚举中,有一个或多个枚举成员拥有该枚举的 其他成员作为相关值。使用递归枚举时,编译器会插入一个中间层。你可以在枚举成员前加上 indirect 来表示这 成员可递归。@H_502_4@
//例如,下面的例子中,枚举类型存储了简单的算数表达式:@H_502_4@
enum ArithmeticExpression {@H_502_4@
case Number(Int)@H_502_4@
indirect case Addition(ArithmeticExpression,ArithmeticExpression)@H_502_4@
indirect case Multiplication(ArithmeticExpression,ArithmeticExpression)@H_502_4@
}@H_502_4@
@H_502_4@
//你也可以在枚举类型开头加上 indirect 关键字来表示它的所有成员都是可递归的:@H_502_4@
indirect enum ArithmeticExpression2 {@H_502_4@
case Number(Int)@H_502_4@
case Addition(ArithmeticExpression2,ArithmeticExpression2)@H_502_4@
case Multiplication(ArithmeticExpression2,ArithmeticExpression2)@H_502_4@
}@H_502_4@
@H_502_4@
//递归函数可以很直观地使用具有递归性质的数据结构。例如,下面是一个计算算数表达式的函数:@H_502_4@
func evaluate(expression: ArithmeticExpression) -> Int {@H_502_4@
switch expression {@H_502_4@
case .Number(let value):@H_502_4@
return value@H_502_4@
case .Addition(let left,let right):@H_502_4@
return evaluate(left) + evaluate(right)@H_502_4@
case .Multiplication(let left,let right):@H_502_4@
return evaluate(left) * evaluate(right)@H_502_4@
}@H_502_4@
}@H_502_4@
@H_502_4@
// 计算(5+4)*2@H_502_4@
let five = ArithmeticExpression.Number(5)@H_502_4@
let four = ArithmeticExpression.Number(4)@H_502_4@
let sum = ArithmeticExpression.Addition(five,four)@H_502_4@
let product = ArithmeticExpression.Multiplication(sum,ArithmeticExpression.Number(2))@H_502_4@
print(evaluate(product))@H_502_4@
@H_502_4@
//该函数如果遇到纯数字,就直接返回该数字的值。如果遇到的是加法或乘法元算,则分别计算左边表达式和右边表达式的值,然后相加或相乘。@H_502_4@