枚举的基本用法

  1. // 定义枚举
  2. enum Direction {
  3. case north
  4. case source
  5. case east
  6. case west
  7. }
  8. // 也可以写成
  9. enum Direction {
  10. case north, source, east, west
  11. }

可以用于switch语句:

  1. let dir: Direction = .north
  2. switch dir {
  3. case .north:
  4. print("north")
  5. case .source:
  6. print("north")
  7. case .east:
  8. print("north")
  9. case .west:
  10. print("north")
  11. }

关联值(Associated Valuse)

有时会将枚举的成员值跟其他类型关联存储在一起,会非常有用

  1. // 分数举例
  2. enum Score {
  3. case point(Int)
  4. case grade(Character)
  5. }
  6. var s = Score.point(96)
  7. s = .grade("A")
  1. // 日期举例
  2. enum Date {
  3. case digit(year: Int, month: Int, day: Int)
  4. case string(String)
  5. }
  6. var date = Date.digit(year: 2018, month: 8, day: 8)
  7. date = .string("2018-8-8")
  8. switch date {
  9. case .digit(let year, let month, let day):
  10. print(year, month, day)
  11. case let .string(value):
  12. print(value)
  13. }

原始值(Raw Values)

枚举成员可以使用相同类型的默认值预先关联,这个默认值叫做:原始值

  1. enum direction: Int {
  2. case left = 0
  3. case right = 1
  4. case up = 2
  5. case down = 3
  6. }
  7. var dir = direction.left
  8. print(dir) // left
  9. print(dir.rawValue) // 0

隐式原始值(Implicitly Assigned Raw Values)

如果枚举的原始值是Int、String,Swift会自动分配原始值

  1. enum Direction: Int {
  2. case north
  3. case source
  4. case east
  5. case west
  6. }
  7. print(Direction.north.rawValue, Direction.source.rawValue) // 0 1
  1. enum Direction: String {
  2. case north
  3. case source
  4. case east
  5. case west
  6. }
  7. print(Direction.north.rawValue, Direction.source.rawValue) // north source

递归枚举

需要使用indirect关键字

  1. indirect enum ArithExpr {
  2. case number(Int)
  3. case sum(ArithExpr, ArithExpr)
  4. case diffrence(ArithExpr, ArithExpr)
  5. }
  6. let five = ArithExpr.number(5)
  7. let four = ArithExpr.number(4)
  8. let sum = ArithExpr.sum(five, four)
  9. let diffrence = ArithExpr.diffrence(sum, four)

MemoryLayout

可以使用MemoryLayout获取数据类型所占内存大小

  1. // 获取类型所占内存
  2. MemoryLayout<Int>.size // 8
  3. MemoryLayout<Int>.stride // 8
  4. MemoryLayout<Int>.alignment // 8
  5. // 获取变量所占内存
  6. var age = 10
  7. MemoryLayout.size(ofValue: age) // 8
  8. MemoryLayout.stride(ofValue: age) // 8
  9. MemoryLayout.alignment(ofValue: age) // 8

获取枚举所占内存大小

  1. /// 带有关联值的枚举
  2. enum Password {
  3. case number(Int, Int, Int, Int)
  4. case other
  5. }
  6. let number = Password.number(1, 2, 3, 4)
  7. let other = Password.other
  8. MemoryLayout<Password>.size // 33,实际用到的空间大小
  9. MemoryLayout<Password>.stride // 40,分配战洪的空间大小
  10. MemoryLayout<Password>.alignment // 8,对齐参数
  1. enum Season: Int {
  2. case spring, summer, autumn, winter
  3. }
  4. MemoryLayout<Season>.size // 1
  5. MemoryLayout<Season>.stride // 1
  6. MemoryLayout<Season>.alignment // 1

原始值不占用枚举变量的内存,占用内存的是spring.rawValue

  1. let s = Season.spring
  2. MemoryLayout.size(ofValue: s.rawValue) // 16
  3. MemoryLayout.stride(ofValue: s.rawValue) // 16
  4. MemoryLayout.alignment(ofValue: s.rawValue) // 8