iOS swift中取枚举类型原始值的方法

来源:互联网 发布:pdf阅读器源码 编辑:程序博客网 时间:2024/06/07 04:41

enum Planet: Int {    case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune}

取swift枚举结构的原始值用toRaw

let earthsOrder = Planet.Earth.toRaw()// earthsOrder is 3

从原始值范围枚举类型用fromRaw

let possiblePlanet = Planet.fromRaw(7)// possiblePlanet is of type Planet? and equals Planet.Uranus

mark:定义枚举类型的时候要把枚举的类型要明确写出,上例中类型是Int

0 0