美文网首页
Swift Enumerations枚举

Swift Enumerations枚举

作者: 点滴86 | 来源:发表于2016-08-08 15:01 被阅读4次

    枚举的定义和使用

    import UIKit
    
    // 枚举的定义和使用
    enum CompassPoint {
        case north
        case south
        case east
        case west
    }
    
    enum Planet {
        case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
    }
    
    var directionToHead = CompassPoint.west
    
    directionToHead = .east
    switch directionToHead {
    case .north:
        print("Lots of planets have a north")
    case .south:
        print("Watch out for penguins")
    case .east:
        print("Where the sun rises")
    case .west:
        print("Where the skies are blue")
    }
    
    let somePlanet = Planet.earth
    switch somePlanet {
    case .earth:
        print("Mostly harmless")
    default:
        print("Not a safe place for humans")
    }
    

    console log 如下


    枚举的定义和使用.png

    关联值

    // 关联值
    enum Barcode {
        case upc(Int, Int, Int, Int)
        case qrCode(String)
    }
    
    var productBarcode = Barcode.upc(1992, 8, 5, 520)
    productBarcode = Barcode.qrCode("ILoveU,Y")
    
    switch productBarcode {
    case .upc(let year, let month, let day, let check):
        print("UPC: \(year)年\(month)月\(day)日\(check)")
    case .qrCode(let productCode):
        print("QR code:\(productCode)")
    }
    

    console log 如下


    关联值.png

    枚举的初始值

    // 枚举的初始值
    enum ASCIIControlCharacter: Character {
        case tab = "\t"
        case lineFeed = "\n"
        case carriageReturn = "\r"
    }
    
    enum AnotherPlanet: Int {
        case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    }
    
    let earthsOrder = AnotherPlanet.earth.rawValue
    print("Earth order is \(earthsOrder)")
    

    console log 如下


    枚举的初始值.png

    初始值初始化枚举值

    // 初始值初始化枚举值
    let positionToFind = 5
    if let somePlanet = AnotherPlanet(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)")
    }
    

    console log 如下


    初始值初始化枚举值.png

    递归枚举

    // 递归枚举
    enum ArithmeticExpression {
        case number(Int)
        indirect case addition(ArithmeticExpression, ArithmeticExpression)
        indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
    }
    
    let four = ArithmeticExpression.number(4)
    let five = ArithmeticExpression.number(5)
    let sum = ArithmeticExpression.addition(four, five)
    let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))
    
    func evaluate(expression: ArithmeticExpression) -> Int {
        switch expression {
        case let .number(value):
            return value
        case let .addition(left, right):
            return evaluate(left) + evaluate(right)
        case let .multiplication(left, right):
            return evaluate(left) * evaluate(right)
        }
    }
    
    print(evaluate(sum))
    print(evaluate(product))
    

    console log 如下


    递归枚举.png

    相关文章

      网友评论

          本文标题:Swift Enumerations枚举

          本文链接:https://www.haomeiwen.com/subject/tqlrsttx.html