
枚举是一组有共同特性的数据的集合,只包含自定义的特定数据
创建枚举
enum Sex {case Malecase Femalecase Unknown}var ss = Sex.Unknownss = .Femaleswitch ss {case .Male:print("男性")breakcase .Female:print("女性")breakcase .Unknown:print("未知")break}
枚举可分为相关值与原始值
关联值
也称“相关值”,由不同数据类型组成,比如 enum {10, 0.8, “Hello”},它的值的创建基于常量或变量。
当你在创建一个基于枚举成员的新常量或变量时才会被设置,并且每次当你这么做得时候,它的值可以是不同的。
enum Student {case Name(String) // 可省略参数名case Score(chinese: Int, math: Int, english: Int)}var stu1 = Student.Name("Jobs")var stu2 = Student.Score(chinese: 98, math: 97, english: 99)switch stu2 {case .Name(let name):print("name is \(name)")case .Score(let chinese, let math, let english):print("score are \(chinese), \(math), \(english)")}
原始值
由相同数据类型组成,比如 enum {10,35,50},它的值的创建基于预先填充的值,是常量。
枚举值类型可为:整型、浮点型、字符串、布尔值
enum Move: Int {case left = 0case right = 1case top = 2case bottom = 3}
在原始值为整数的枚举时,不需要显式的为每一个成员赋值,Swift 会自增为你赋值,默认从 0 开始自增。
enum Month: Int {case January = 1,February, March, April, May, June, July, August, September, October, November, December}print("Month is \(Month.September.rawValue)")
如果使用原始值类型定义枚举,则枚举会自动接收一个带有原始值的初始化器,并返回可选类型的枚举实例。
使用枚举的 rawValue 属性来访问其原始值。
enum MethodType: String {case GET = "get"case POST = "post"case PUT}print(MethodType.GET.rawValue) // "get"print(MethodType.PUT.rawValue) // "PUT"let type = MethodType(rawValue: "post")print(type!) // POST
高级用法
嵌套枚举
enum Area {case BeiJingcase ShangHaienum JiangSu {case NanJingcase SuZhoucase WuXi}}print(Area.JiangSu.SuZhou)
以下内容超纲,可以之后再回过来看
方法和属性
enum Device {case iPad, iPhone, AppleTV, AppleWatch/**计算属性增加一个存储属性到枚举中不被允许,但你依然能够创建计算属性。当然,计算属性的内容都是建立在枚举值下或者枚举关联值得到的。*/var productName: String {switch self {case .iPad: return "iPad"case .iPhone: return "iPhone"case .AppleWatch: return "AppleWatch"case .AppleTV: return "AppleTV"}}// 方法func description() -> String {switch self {case .iPad: return "iPad"case .iPhone: return "iPhone"case .AppleWatch: return "AppleWatch"case .AppleTV: return "AppleTV"}}// 静态方法static func getTypeWithName(name: String) -> Device? {if name == "iWatch" {return .AppleWatch}return nil}}let device = Device.iPhoneprint(device.productName)print(device.description())print(Device.getTypeWithName(name: "iWatch")!)
enum Rotate: Int {case Topcase Rightcase Bottomcase Left/**enum 中可以定义可变方法,这样就可以更改 self 的值*/mutating func next() {switch self {case .Top:self = .Rightcase .Right:self = .Bottomcase .Bottom:self = .Leftcase .Left:self = .Top}}}
协议
protocol PrintProtocol {var description: String? { get }}enum Trade: PrintProtocol {case Buy(stock: String, amount: Int)case Sell(stock: String, amount: Int)var description: String? {switch self {case .Buy(_, _):return "Trade.Buy..."case .Sell(_, _):return "Trade.Sell..."}return nil}}let trade = Trade.Buy(stock: "car", amount: 1)print(trade.description!)
扩展
protocol PrintProtocol {var description: String? { get }}enum Trade {case Buy(stock: String, amount: Int)case Sell(stock: String, amount: Int)}extension Trade: PrintProtocol {var description: String? {switch self {case .Buy(_, _):return "Trade.Buy..."case .Sell(_, _):return "Trade.Sell..."}return nil}}let trade = Trade.Buy(stock: "car", amount: 1)print(trade.description!)
泛型
enum Product<T> {case price(T)func getPrice() -> T {switch self {case .price(let value):return value}}}let prod = Product<Int>.price(100)print(prod.getPrice())
备注:[枚举]
