swift:Enumeration 枚举

枚举 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)结合的值 或者QRCode和string类型结合的值
    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标志
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值可以找到对应的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"







你可能感兴趣的:(swift:Enumeration 枚举)