zoukankan      html  css  js  c++  java
  • Swift教程之枚举

    枚举

    C语言中,C枚举将相关名称分配给一组整数值,而Swift枚举不必为每个枚举值提供一个值。若为每个枚举值提供了值(称为原始值),则该值可以是字符串、字符或任何整数或浮点类型的值。

    枚举还可以指定与每个枚举值一起存储的任何类型的关联值,这与其他语言中的联合或变体一样。

    Swift中的枚举拥有传统上仅由类支持的许多功能,例如计算属性来提供当前枚举值的附加信息,以及实例方法来提供与枚举值的相关功能。


    ## 枚举语法 使用**Enum**关键字声明枚举:
    enum SomeEnumeration {
        // enumeration definition goes here
    }
    

    以下是指南针的四个方向的例子:

    enum CompassPoint {
        case north
        case south
        case east
        case west
    }
    

    使用case关键字引入新的枚举情况。

    注意

    与C和Objective-C不同,Swift的枚举值不会在创建时分配默认整数值。上述枚举值不会隐式地等于0、1、2、3。

    多个case可以写在一行,用逗号隔开:

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

    每个枚举都是一个全新的类型,像其它类型一样,名称以大写字母开头,且为单数而非复数名称。

    var directionToHead = CompassPoint.west
    

    使用枚举类型将某个枚举值初始化时,Swift会推断其具体枚举类型,下次重新赋值时,可忽略枚举类型。使用点语法:

    directionToHead = .east
    

    ## 使用Switch语句匹配枚举值 使用**switch**语句匹配单个枚举值:
    directionToHead = .south
    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")
    }
    // Prints "Watch out for penguins"
    

    switch语句必须包括所有没有枚举值,否则无法通过编译,确保某个枚举情况不会被意外省略。

    使用default涵盖未明确处理的任何枚举值:

    let somePlanet = Planet.earth
    switch somePlanet {
    case .earth:
        print("Mostly harmless")
    default:
        print("Not a safe place for humans")
    }
    // Prints "Mostly harmless"
    

    ## 关联值 有时可以将其他类型的值和枚举值一起关联起来,与枚举值一起存储,并在每次使用该信息时,该信息会发生变化。

    以下为携带关联值的条形码枚举:

    enum Barcode {
        case upc(Int, Int, Int, Int)
        case qrCode(String)
    }
    

    关联值不提供类型的实际值,只提供枚举值存储的关联值的类型。

    使用一下任一类型创建枚举值:

    var productBarcode = Barcode.upc(8, 85909, 51226, 3)
    
    productBarcode = .qrCode("ABCDEFGHIJKLMNOP")
    

    使用switch提取关联值:

    switch productBarcode {
    case .upc(let numberSystem, let manufacturer, let product, let check):
        print("UPC: (numberSystem), (manufacturer), (product), (check).")
    case .qrCode(let productCode):
        print("QR code: (productCode).")
    }
    // Prints "QR code: ABCDEFGHIJKLMNOP."
    

    关联值全被提取为常量或变量:

    switch productBarcode {
    case let .upc(numberSystem, manufacturer, product, check):
        print("UPC : (numberSystem), (manufacturer), (product), (check).")
    case let .qrCode(productCode):
        print("QR code: (productCode).")
    }
    // Prints "QR code: ABCDEFGHIJKLMNOP."
    

    ## 原始值 枚举值可以使用类型一致的默认值(即**原始值**)填充。
    enum ASCIIControlCharacter: Character {
        case tab = "	"
        case lineFeed = "
    "
        case carriageReturn = "
    "
    }
    

    原始值可以是字符串、字符或任何整数或浮点数类型,每个原始值必须是唯一的。

    隐式分配枚举值

    不必为每个枚举值显示分配原始值,Swift会自动分配。

    整数用于原始值时,第一个枚举值自动设置为0,之后递增1。

    enum Planet: Int {
        case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    }
    

    上面例子中,Planet.Planet具有显式原始值1,其他为隐式枚举值是在其基础上递增1。

    字符串作为原始值时,每个枚举值的隐含原始值是该枚举值的字符串字面量。

    enum CompassPoint: String {
        case north, south, east, west
    }
    

    上面例子中,CompassPoint.south的隐式原始值为“south”,以此类推。

    使用枚举类型的rawValue属性获取其枚举值的原始值:

    let earthsOrder = Planet.earth.rawValue
    // earthsOrder is 3
     
    let sunsetDirection = CompassPoint.west.rawValue
    // sunsetDirection is "west"
    

    用原始值初始化枚举值

    若使用原始值类型定义枚举,枚举会自动创建一个初始化器,并可通过原始值的初始化器创建枚举的实例。

    let possiblePlanet = Planet(rawValue: 7)
    // possiblePlanet is of type Planet? and equals Planet.uranus
    

    然而,并不是所有的原始值都能找到匹配的枚举值,所以原始值初始化器(可失败的初始化器)总是返回枚举的可选类型,可结合可选绑定和switch语句处理该可选类型。

    let positionToFind = 11
    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)")
    }
    // Prints "There isn't a planet at position 11"
    

    ## 递归枚举 当某个枚举值的关联值类型是该枚举类型时,该枚举是递归枚举。

    递归枚举有两种书写方式:

    1. 在需要递归的枚举值前加上indirect关键字,给部分枚举值启用递归。

    `

    enum ArithmeticExpression {
        case number(Int)
        indirect case addition(ArithmeticExpression, ArithmeticExpression)
        indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
    }
    

    2.在声明枚举类型enum关键字前加上indirect关键字,给所有枚举值启用递归。

    indirect enum ArithmeticExpression {
        case number(Int)
        case addition(ArithmeticExpression, ArithmeticExpression)
        case multiplication(ArithmeticExpression, ArithmeticExpression)
    }
    

    创建上述递归枚举实例:

    let five = ArithmeticExpression.number(5)
    let four = ArithmeticExpression.number(4)
    let sum = ArithmeticExpression.addition(five, four)
    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(product))
    // Prints "18"
  • 相关阅读:
    重定向请求
    json处理
    post请求
    get请求
    提交cookie登录
    进击的Python【第三章】:Python基础(三)
    进击的Python【第二章】:Python基础(二)
    进击的Python【第一章】:Python背景初探与Python基础(一)
    java 内存分析
    java--循环练习
  • 原文地址:https://www.cnblogs.com/keqipu/p/7624328.html
Copyright © 2011-2022 走看看