Swift学习 --- 2.8枚举

<span style="font-size:18px;">

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

enum Planet {
    case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Nepturn
}

//枚举多种类型
enum Barcode {
    case UPCA(Int, Int, Int)
    case QRCode(String)
}

//原始值 可以定义为任意类型的原始值
enum ASCIIControlCharacter: Character {
    case Tab = "\t"
    case LineFeed = "\n"
    case CarriageReturn = "\r"
}

// 原始值可以自动递增
enum Planet1: Int {
    case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
}

class ViewController: UIViewController {
                            
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        //赋值不能单纯的等于 directionToHead = CompassPoint 要等于里面的一个值
        var directionToHead = CompassPoint.East
        directionToHead = .West
        println(directionToHead)
        
        //枚举可以有多个值是同一个选项
        let somePlanet = Planet.Mars
        switch somePlanet {
        case .Mars:
            println("Mostly harmless")
        default:
            println("Not a safe place for humans")
        }
        
        var productBarcode = Barcode.UPCA(8, 85909_51226, 3)

        //前缀一个let 简便书写
        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).")
        }
        // 输出 "QR code with value of ABCDEFGHIJKLMNOP."
        
        //原始值 感觉这个比较有用
        let earthsOrder = Planet1.Earth.toRaw() //使用枚举成员的toRaw方法可以访问该枚举成员的原始值:
        let possiblePlanet = Planet1.fromRaw(7)
        
        println(earthsOrder)
        println("\(possiblePlanet)")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}




</span>

你可能感兴趣的:(ios,swift)