Swift5.0笔记 - 4. 枚举

2020-11-10  本文已影响0人  南城同學

1) 定义

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

enum Direction {
    case north, south, east, west
}

2) 使用

var dir = Direction.west
dir = Direction.east
dir = .north
print(dir)//noth

switch dir {
case .north:
    print("north")
case .south:
    print("north")
case .east:
    print("north")
case .west:
    print("north")
}

3) 关联值(Associated Values)

enum Score {
    case points(Int)
    case grade(Character)
}

var score = Score.points(99)
score = .grade("A")

switch score {
case let .points(i):
    print(i, "points")
case let .grade(i):
    print("grade", i)
} //grade A
enum Date {
    case digit(year: Int, month: Int, day: Int)
    case string(String)
}

var date = Date.digit(year: 2020, month: 9, day: 22)
date = .string("2020-9-22")
switch date {
case .digit(let year, let month, let day): //let .digit(year, month, day)
    print(year, month, day)
case let .string(value):
    print(value)
}

4) 原始值(Raw Value)

enum PokerSuit : Character {
    case spade = "♠️"
    case heart = "♥️"
    case diamond = "♦️"
    case club = "♣️"
}
var suit = PokerSuit.spade
print(suit) // spade
print(suit.rawValue) // ♠️
print(PokerSuit.club.rawValue)//♣️
enum Grade : String {
case perfect = "A"
case great = "B"
case good = "C"
case bad = "D"
}
print(Grade.perfect.rawValue) // A
print(Grade.great.rawValue) // B
print(Grade.good.rawValue) // C
print(Grade.bad.rawValue) // D

5) 隐式原始值(Implicity Assigned Raw Value)

  1. String类型
enum Direction : String {
    case north = "north"
    case south = "south"
    case east = "east"
    case west = "west"
}

// 等价于
enum Direction : String {
    case north, south, east, west
}
print(Direction.north) // north
print(Direction.north.rawValue) // north

  1. Int类型
enum Season : Int {
    case spring, summer, autumn, winter
}
print(Season.spring.rawValue) // 0
print(Season.summer.rawValue) // 1
print(Season.autumn.rawValue) // 2
print(Season.winter.rawValue) // 3

6) 递归枚举(Recursive Enumeration)

indirect enum ArithExpr {
    case number(Int)
    case sum(ArithExpr, ArithExpr)
    case difference(ArithExpr, ArithExpr) }
enum ArithExpr {
    case number(Int)
    indirect case sum(ArithExpr, ArithExpr)
    indirect case difference(ArithExpr, ArithExpr) }
let five = ArithExpr.number(5)
let four = ArithExpr.number(4)
let two = ArithExpr.number(2)
let sum = ArithExpr.sum(five, four)
let difference = ArithExpr.difference(sum, two)
func calculate(_ expr: ArithExpr) -> Int {
    switch expr {
    case let .number(value):
        return value
    case let .sum(left, right):
        return calculate(left) + calculate(right)
    case let .difference(left, right):
        return calculate(left) - calculate(right)
    } }
calculate(difference)

7) MemoryLayout

enum Password {
case number(Int, Int, Int, Int)
case other
}
MemoryLayout<Password>.stride // 40, 分配占用的空间大小
MemoryLayout<Password>.size // 33, 实际用到的空间大小
MemoryLayout<Password>.alignment // 8, 对齐参数
var pwd = Password.number(9, 8, 6, 4)
pwd = .other
MemoryLayout.stride(ofValue: pwd) // 40
MemoryLayout.size(ofValue: pwd) // 33
MemoryLayout.alignment(ofValue: pwd)//8

上一篇下一篇

猜你喜欢

热点阅读