Swift学习笔记enum

enum CompassPoint {
    case North
    case South
    case East
    case West
}

enum Planet{
    case Mercury, Venus, Earth, Mars
}

var directionToHead = CompassPoint.West
directionToHead = .East

switch directionToHead {
case .North:
    println("Lots of planets have a north")
case .South:
    println("Watch out for penguins")
case .East:
    println("Where the sun Rises")
case .West:
    println("Where the skies are blue")
default:
    println("Not a safe place for humans")
}

enum Barcode{
    case UPCA(Int, Int, Int)
    case QRCode(String)
}

var productBarcode = Barcode.UPCA(8, 85909_51226, 3)
productBarcode = .QRCode("UPCA")

switch productBarcode{
case .UPCA(let numberSystem, let identifier, let check):
  println("UPC-A with value of \(numberSystem), \(identifier), \(check).")
case .QRCode(let productCode):
    println("QR code with value of \(productCode).")
}


switch productBarcode{
case let .UPCA(numberSystem, identifier, check):
    println("UPC-A with value of \(numberSystem), \(identifier), \(check).")
case let .QRCode( productCode):
    println("QR code with value of \(productCode).")
}

enum ASCIIControlCharacter: Character{
    case Tab = "\t"
    case LineFeed = "\n"
    case CarriageReturn = "\r"
}

enum Planets: Int {
    case mercury = 1, Venus
}
let earthsOrder = Planets.mercury.rawValue

以上是枚举的一般用法,限定枚举原始类型,与switch的结合,获取枚举的原始值等。

你可能感兴趣的:(swift)