swift4.03 学习笔记(9)

枚举

枚举语法

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

也可以写成单行

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

使用switch匹配枚举

let 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“

相关值

给枚举定义相关值

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."

如果要取出枚举的所有相关值,可以把let或var写在前面

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."

Raw Values(原生值)

原生值的类型可以是字符串,字符,整形或浮点型,定义字符型的原生值

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
    
}

let earthsOrder = Planet.earth.rawValue
// earthsOrder is 3

第一个枚举的原生值指定为1,第二个枚举自动赋值为2,第三个为3,依此类推,直到对所有枚举自动赋值。

定义字符串类型枚举:

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

枚举的原生值默认为枚举名的字符串

let sunsetDirection = CompassPoint.west.rawValue
// sunsetDirection is "west"

使用原生值初始化话枚举

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

如果对应的原生值不存在,返回nil

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"

Recursive Enumerations(递归枚举)

一个或两个枚举的相关值是其他或自身枚举。在case前使用indirect关键字

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

或者在enum前使用关键字indirect

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

使用ArithmeticExpression实现(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))

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"

你可能感兴趣的:(swift4.03 学习笔记(9))