Swift - 39 - 枚举类型关联默认值
//: Playground - noun: a place where people can play import UIKit enum Month: Int { // 这么定义, 后面的Feb, Mar会自动赋值为2和3.. case Jan = 1, Feb, Mar, Apr, May, Jun, July, Aug, Sep, Oct, Nov, Dec } let currentMonth:Month = .Nov // 查看枚举对应的值 currentMonth.rawValue // 通过这种方法创建出来的枚举变量是一个可选型变量..需要解包后使用 let nextMonth = Month(rawValue: 12) nextMonth!.rawValue // 示例 var userInputNumber = 0 if let theMonth = Month(rawValue: userInputNumber) { switch theMonth { case .Jan: print("It's the first month") case .Dec: print("It's the last month") default: print("It's the normal month") } } else { print("ERROR") } // swift中关联的默认值不仅仅是整数, 也可以字符串等其他类型 enum PersonNum: String { case one = "Rinpe" case two = "Lili" case three = "Lolo" } // 示例 let userInput = "Rinpe" if PersonNum(rawValue: userInput) != nil { print("欢迎您, \(userInput)教授") } else { print("对不起, 您不是本公司的员工") }