// 枚举的定义enum Day { case sunday case monday case tuesday case wednesday case thursday case frIDay case saturday}let day = Day.mondayfunc getDayStatus(day:Day)->VoID { switch day { case Day.sunday,Day.saturday: print("Rest Day") break default: print("Work Day") break }}getDayStatus(day: day)
给枚举附上原始值
// 枚举的定义,给枚举附上原始值enum Day: Int { case sunday = 0 case monday = 1 case tuesday = 2 case wednesday = 3 case thursday = 4 case frIDay = 5 case saturday = 6}func daysBeforeNewWeek(day:Day) ->Int { return 7 - day.rawValue}daysBeforeNewWeek(day: Day.monday) // 离新的星期还有几天
枚举可以有方法,这就厉害了我的Swift
// 枚举的定义,给枚举附上原始值enum Day: Int { case sunday = 0 case monday = 1 case tuesday = 2 case wednesday = 3 case thursday = 4 case frIDay = 5 case saturday = 6 func isWorkDay() -> Bool { switch self { case .monday,.tuesday,.wednesday,.thursday,.frIDay: return true default: return false } }}var day = Day.frIDayday.isWorkDay()总结
以上是内存溢出为你收集整理的Swift 枚举全部内容,希望文章能够帮你解决Swift 枚举所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)