swift -- 枚举

1.枚举语法

enum Point {

     case x

     case y

}

 2.使用 switch 语句匹配枚举值

let po = Point.x

switch po {

case .x : print("x")    //case 后面条件可以直接 点 .x

case .y : print("y")

}

 3.枚举原始值

enum Bounds : Int { //约束枚举值为Int类型

    case width = 10

    case height = 20

}

print(Bounds.width.rawValue)     //rawValue 获取到那个值

4.枚举原始值的隐式赋值

enum Char :Int {

    case a = 1,b,c,d,e,f   //b c d 的值会自动加 1

 }

print(Char.d.rawValue)

5.使用原始值初始化枚举实例

let c = Char.a

print(c)

let d = Char.init(rawValue: 4)

print(d!)

原文地址:https://www.cnblogs.com/daxueshan/p/5585222.html