iOS Developer

swift入门7 枚举

2017-04-14  本文已影响24人  C93zzo

枚举

枚举语法

You introduce enumerations with the enum keyword and place their entire definition within a pair of braces:

枚举用enum关键字开始,后面加上大括号括起来的枚举定义:

    enum SomeEnumeration {
        // 枚举的定义
    }

举个例子

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

Unlike C and Objective-C, Swift enumeration cases are not assigned a default integer value when they are created. In the CompassPoint example above, north, south, east and west do not implicitly equal 0, 1, 2 and 3. Instead, the different enumeration cases are fully-fledged values in their own right, with an explicitly-defined type of CompassPoint.

跟 c oc不同的是,swift中的枚举的case在创建的时候并不会赋予一个默认的整数值。
比如上面的CompassPoint的例子,north, south, east 和 west并不是隐式得等于0,1,2,3. 它们的值就是它们本身,类型为CompassPoint。

关联值

You can define Swift enumerations to store associated values of any given type, and the value types can be different for each case of the enumeration if needed.

你可以用swift的枚举来存储任何给定类型关联值,枚举的case的值类型可以互不相同。

For example, suppose an inventory tracking system needs to track products by two different types of barcode. Some products are labeled with 1D barcodes in UPC format, which uses the numbers 0 to 9. Each barcode has a “number system” digit, followed by five “manufacturer code” digits and five “product code” digits. These are followed by a “check” digit to verify that the code has been scanned correctly:

举个例子,假设一个库存查询系统需要通过两种不同的条形码来查询产品。有些产品是用upc格式的1d 条形码—使用从0到9的数字—标记的。每个条形码有一位“数字系统”,后面跟着5位“工厂代码”和5位“产品代码”。后面再跟1位“检查”位以标记此条形码是否已被正确扫描。

Other products are labeled with 2D barcodes in QR code format, which can use any ISO 8859-1 character and can encode a string up to 2,953 characters long:

其他产品用二维码的形式来标记,

It would be convenient for an inventory tracking system to be able to store UPC barcodes as a tuple of four integers, and QR code barcodes as a string of any length.

对于一个库存查询系统来说,能够把upc条形码以包含4个整数的tuple,把二维码以任意长度的字符串来存储的话就会非常方便。

上面的例子的枚举:

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

这段代码的含义是:

定义一个叫Barcode的枚举,有两个值,一个是关联值为 (Int, Int, Int, Int)类型的upc,一个关联值为string类型的qrCode。

举个例子

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

This example creates a new variable called productBarcode and assigns it a value of Barcode.upc with an associated tuple value of (8, 85909, 51226, 3).

这个例子创建了一个新的变量productBarcode,并且赋了一个Barcode.upc给它,该Barcode.upc有一个tuple的关联值(8, 85909, 51226, 3)

这个产品还能赋一个不同类型的条形码:

productBarcode = .qrCode("ABCDEFGHIJKLMNOP")

递归枚举

递归枚举要在前面加上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)
}

如果你觉得文章不错,可以给我打赏点比特股(bts),以示支持。_

BTS6jUaVVkz9gN8t9sWY9NR5UbiubSz7QtVDnEtFGpujYeqQSfQ5E

上一篇下一篇

猜你喜欢

热点阅读