一 .枚举基本的写法和使用
enum Month {
case January
case February
case March
case April
case May
case June
case July
case Auguest
case September
case October
case November
case December
}
print(Month.January)
let curMonth = Month.July
/**
有限值 枚举是一种新的类型
*/
func season(month:Month) -> String {
switch month {
case Month.March,Month.April,Month.May:
return "Spring"
case Month.June,Month.July,Month.Auguest:
return "Summer"
case Month.September,Month.October,Month.November:
return "Autumn"
case .December,Month.January,Month.February:
return "Spring"
// default:
// return "Not valid"
}
/**
* 枚举类型的话,可以被穷举 ,Default 可以去掉
*/
}
二 .枚举与原始值
enum Month :Int{
case January = 1
case February
case March
case April
case May
case June
case July
case Auguest
case September
case October
case November
case December
}
print(12-Month.January.rawValue)
let curMonth = Month.July
func monthsBeforeNewYear(month:Month) -> Int {
return 12 - month.rawValue
}
let input = 11;
let month = Month(rawValue: input) // int 类型 和 枚举类型之间的转化 //返回值是一个可选型
if let month = month{ //解包操作
monthsBeforeNewYear(curMonth)
}
// 枚举 和原始值得简写
enum Grade:Int{
case F,E,D,C,B,A
}
let grade:Grade = .A
print("Your score is \(grade.rawValue)")
// 枚举原始值 可以不按顺序 这种写法给了我们很大的使用空间
enum Coin:Int {
case Penny = 1
case Nickel = 5
case Dime = 10
case Quarter = 25
}
let coin:Coin = .Quarter
print("It's \(coin.rawValue) cents")
// 枚举原始值 可以默认推导
enum ProgrammingLanguage:String {
case Swift
case ObjectiveC = "Objective-C"
case C
case Java
}
let myFavoriteLanguage:ProgrammingLanguage = .ObjectiveC
print("\(myFavoriteLanguage.rawValue) is my FavoriteLanguage")
三,枚举中 关联数值的使用 有多态既视感的运用,炫酷
// Associate Value 原始值 和 关联值 不能同时设置
enum ATMStatus {
case Success(Int)
case Error(String)
case Warning //也可以不设置关联值
}
var balance = 1000
func withdraw(amount:Int)->ATMStatus{
if balance >= amount{
balance -= amount
return .Success(balance)
}else{
return .Error("Not enough money")
}
}
let result = withdraw(10000) //表示提款 100
/**
* 解析值 不是必须的.
*/
switch result{
case let .Success(newBalance): //这里 有一个解包的过程 值会放在newBalance 中,新语法
print("\(newBalance) Yan left in you account")
case let .Error(errorMessge):
print("Error:\(errorMessge)")
case .Warning:
print("Warning")
}
/**
* 不解析
*/
switch result{
case .Success:
print("Success")
case .Error:
print("Error")
case .Warning:
print("Warning")
}
/**
枚举类型 关联多个值 的一个应用, 有那种多态的既视感
*/
enum Shape {
case Square(side:Double)
case Rectangle(width:Double,height:Double)
case Circle(centerx:Double,centery:Double,radius:Double)
case Point
}
let square = Shape.Square(side: 10)
let rectangel = Shape.Rectangle(width: 20, height: 30)
let circle = Shape.Circle(centerx: 0, centery: 0,radius: 10)
let point = Shape.Point
func area(shape:Shape)-> Double{
switch shape {
case let .Square(side):
return side*side
case let .Rectangle(width,height):
return width * height
case let .Circle(_,_,r): //下划线 忽略参数
return M_PI*r*r
case .Point:
return 0
//
// default:
// return 0
}
}
area(square)
area(rectangel)
area(circle)
area(point)
四.可选型和枚举类型的 渊源
var age:Int? = 17
print(age)
/**
* enum Optional : _Reflectable, NilLiteralConvertible
点击查看发现 Optional 就是 enum 枚举类型 中的关联数据,包含
enum Option{
Some(AnyObject)
None
}
Some 是Optional 一个可选性
*/
var website:Optional = Optional.Some("www.baidu.com")
//
website = "www.baidu.com"
//website = nil
//website = .None
switch website{
case .None:
print("No website")
case let .Some(website):
print("The website is \(website)")
}
/// 解包 来源于 上面swich 的赋值 简写
if let website = website{
print("The website is \(website)")
}
五. 枚举关联数据类型 使用递归的变态实例
/**
递归声明
添加关键字 indirect
*/
indirect enum ArithmeticExpression {
case Number(Int)
case Addition(ArithmeticExpression,ArithmeticExpression)
case Multiplication(ArithmeticExpression,ArithmeticExpression)
}
let five = ArithmeticExpression.Number(5)
let four = ArithmeticExpression.Number(4)
let two = ArithmeticExpression.Number(2)
let sum = ArithmeticExpression.Addition(five, four)
let product = ArithmeticExpression.Multiplication(sum, two)
func evaluate(expression:ArithmeticExpression) -> Int {
switch expression {
case let .Number(value):
return value
case let .Addition(left,right):
return evaluate(left)+evaluate(right)
case let .Multiplication(left,right):
return evaluate(left)*evaluate(right)
}
}
evaluate(product)