• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift_枚举

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

点击查看源码

空枚举

//空枚举
enum SomeEnumeration {
    // enumeration definition goes here
}

枚举基本类型

//枚举基本类型
enum CompassPoint {
    case north
    case south
    case east
    case west
}

简写

//简写
enum Planet {
    case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}

枚举语法

//枚举语法
func testEnumerationSyntax() {
    
    //使用
    var directionToHead = CompassPoint.west
    //可不写 前面的枚举名
    directionToHead = .east
    print("\(directionToHead)")
    
    /*  print
     
     east
     
     */
}

枚举匹配

//枚举匹配
func testMatchingEnumeration() {
    
    var directionToHead = CompassPoint.south
    
    //if匹配
    if directionToHead == CompassPoint.south {
        directionToHead = .east
    }
    
    //switch匹配
    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")
    default:
        print("default")
    }
    
    /*  print
     
     Where the sun rises
     
     */
}

关联值

//关联值
func testAssociatedValues() {
    //枚举可以和结构体类型的数据关联使用
    enum Barcode {
        case upca(Int, Int, Int, Int)
        case qrCode(String)
    }
    
    // 初始化
    var productBarcode = Barcode.upca(8, 85909, 51226, 3)
    productBarcode = .qrCode("ABCDEFGHIJKLMNOP")
    
    //匹配
    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).")
    }
    
    //可以不写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).")
    }
    
    /*  print
     
     QR code: ABCDEFGHIJKLMNOP.
     QR code: ABCDEFGHIJKLMNOP.
     
     */
}

原始值

//原始值
func testRawValues() {
    enum ASCIIControlCharacter: Character {
        case tab = "\t"
        case lineFeed = "\n"
        case carriageReturn = "\r"
    }
    
    //隐式分配原始值
    enum Planet: Int {
        case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    }
    
    //原始值为属性名转换
    enum CompassPoint: String {
        case North, South, East, West
    }
    
    print("\(Planet.earth.rawValue)")
    print("\(CompassPoint.West.rawValue)")
    
    // 通过原始值初始化
    let possiblePlanet = Planet(rawValue: 7)
    print("\(possiblePlanet)")
    let positionToFind = 9
    print("\(possiblePlanet)")
    
    // 当原始值不匹配时,返回为nil
    if let somePlanet = Planet(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)")
    }
    
    /*  print
     
     3
     West
     Optional(Swift_枚举.(testRawValues () -> ()).(Planet #1).uranus)
     Optional(Swift_枚举.(testRawValues () -> ()).(Planet #1).uranus)
     There isn't a planet at position 9
     
     */
}

枚举循环

//枚举循环
func testRecursiveEnumerations() {
    //indirect循环关键字
    //            enum ArithmeticExpression {
    //                case Number(Int)
    //                indirect case Addition(ArithmeticExpression, ArithmeticExpression)
    //                indirect case Multiplication(ArithmeticExpression, ArithmeticExpression)
    //            }
    
    // 可将indirect写到枚举前
    indirect enum ArithmeticExpression {
        case number(Int) // 值
        case addition(ArithmeticExpression, ArithmeticExpression)       // 加
        case multiplication(ArithmeticExpression, ArithmeticExpression) // 乘
    }
    
    // 函数使用
    func evaluate(_ expression: ArithmeticExpression) -> Int {
        switch expression {
        case .number(let value):
            return value
        case .addition(let left, let right):
            return evaluate(left) + evaluate(right)
        case .multiplication(let left, let right):
            return evaluate(left) * evaluate(right)
        }
    }
    
    // evaluate (5 + 4) * 2
    let five = ArithmeticExpression.number(5)
    let four = ArithmeticExpression.number(4)
    let sum = ArithmeticExpression.addition(five, four)
    let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))
    print(evaluate(product))
    
    /*  print
     
     18
     
     */
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift学习与复习发布时间:2022-07-13
下一篇:
swift与NSObject发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap