枚舉 rawValue indirect
//Enumerations 枚舉
//enumeration syntax 枚舉語法 枚舉的第一個單詞大寫
enum SomeEnumeration {
//enumeration definition goes here 枚舉的定義
}
//枚舉的case 在定義的時候沒初始值 這點是與OC 不同的地方 也就是說North不等于0 South不等于1 鼠次。粗悯。。
enum CompassPoint{
case North
case South
case East
case West
}
//enumeration 的cases 在同一行的時候 用逗號隔開
enum Planet{
case Mercury, Venus, Earth, Mars, Jupiter , Saturn, Uranus, Neptune海王星
}
var directionTohead = CompassPoint.West
directionTohead = .West //directionToHead 在初始化為CompassPoint的某個值時, 可是省略枚舉的名稱使用點語法
//通過switch語句來匹配枚舉的值 沒有其他情況的時候不需要使用default
switch directionTohead{
case .North:
print("Lots of planets have a north")
case .South:
print("Watch out fir penguins")
case .East:
print("Where is sun rises")
case .West:
print("Where the skies are blue") //"Where the skies are blue\n"
}
//switch 使用default的時候
let somePlanet = Planet.Earth
switch somePlanet{
case .Earth:
print("Mostly harmless") //"Mostly harmless\n"
default:
print("Not a safe place for human")
}
//case后面沒有冒號的情況
enum Barcode{
case UPCA(Int, Int, Int, Int)//代表是 Barcode枚舉 可以拿到UPCA和type(Int, Int, Int, Int)結(jié)合的值 或者QRCode和string類型結(jié)合的值
case QRCode(String)
}
var productBarcode = Barcode.UPCA(8, 85909, 51226, 3) //UPCA(8, 85909, 51226, 3)
productBarcode = .QRCode("ABCDEFGHIJKLMOP")//QRCode("ABCDEFGHIJKLMOP")
switch productBarcode {
case .UPCA(let numberSystem, let manufacturer, let product, let check):
print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).")
case .QRCode(let productCode):
print("QR code: \(productCode).") //"QR code: ABCDEFGHIJKLMOP.\n"
}
//如果枚舉的cases 所有元素作為常量 或者全部的元素都是變量 那么可是在case的名字前有var或者let標(biāo)志
switch productBarcode {
case let .UPCA(numberSystem, manufacturer, product, check):
print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).")
case let .QRCode(productCode):
print("QR code: \(productCode).")
}
// Prints "QR code: ABCDEFGHIJKLMNOP."
//Raw Values 給case的名稱賦初始值
enum ASCIIControlCharacter: Character{
case Tab = "\t"
case LineFeed = "\n"
case CarriageReturn = "\r"
}
ASCIIControlCharacter.Tab.rawValue//" "
enum Planets2: Int{
case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune //Planets2.Mercury有個明確的值是1悲立,那么Mercury.Earth有個隱式的值是2以此類推
}
Planets2.Earth.rawValue //3
Planets2.Mercury.rawValue//1
//枚舉是string類型的代表 case語句被賦了初始值
enum CompassPoint1: String {
case North = "Northe", South, East, West
}
CompassPoint1.West.rawValue //"West"
CompassPoint1.North.rawValue //"Northe"
enum CompassPoint2: String {
case North, South, East, West
}
CompassPoint2.West.rawValue //"West"
CompassPoint2.North.rawValue //"North"
//帶初始值初始化 initializing from a raw value
let possiblePlanet = Planets2(rawValue: 7) //Uranus 并不是所有的int值可以找到對應(yīng)的planet 所以raw value initializer總是返回一個可選的枚舉的case 所以 possiblePlanet 是Planets2? 或者是optional Planets2 如果rawVlaue: 11 那么Planets2(rawValue: 11) 返回的就是空nil
let positionToFind = 11
if let somePlanet = Planets2(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"
//recursive Ennumerations 遞歸枚舉 indirect 間接的
enum ArithmeticExpression{
case Nunber(Int)
indirect case Addition(ArithmeticExpression, ArithmeticExpression)
indirect case Multiplication(ArithmeticExpression, ArithmeticExpression)
}
//如果所有的case語句需要indirect的話 也可以吧indirect 放在 枚舉的前面 enum的前面
indirect enum ArithmeticExpression1{
case Number(Int)
case Addition(ArithmeticExpression1,ArithmeticExpression1)
case Mutiplication(ArithmeticExpression1, ArithmeticExpression1)
}
let thre = ArithmeticExpression1.Number(3)
let four = ArithmeticExpression1.Number(4)
let sum = ArithmeticExpression1.Addition(thre, four)
let product = ArithmeticExpression1.Mutiplication(thre, four)
func evaluate(expression: ArithmeticExpression1) -> Int {
switch expression {
case let .Number(value):
return value
case let .Addition(left, right):
return evaluate(left) + evaluate(right)
case let .Mutiplication(left, right):
return evaluate(left) * evaluate(right)
}
}
print(evaluate(product))
// Prints "12"