Swift-枚举
2018-01-12 本文已影响0人
qtoq126
- 基础例子
enum Month {
case January
case Feberuay
case March
case April
case May
case June
case July
case Augest
case September
case Octorber
case November
case December
}
//两种方法是相同对
var curMonth = Month.January
var NextMonth: Month = .Feberuay
enum Season {
case Spring, Summer, Autumn, Winter //可以只写一个case,然后后逗号隔开
}
func season(month: Month) -> Season {
switch month {
case .January, .Feberuay, .March:
return .Spring
case .April, .May, .June:
return .Summer
case .July, .Augest, .September:
return .Autumn
case .Octorber, .November, .December:
return .Winter
}
}
let curSeason = season(month: curMonth)
- 原始值(Raw Value)
//枚举变量是默认值
enum Grade: Int { //定义初始值类型
case F, E, D, C, B, A //如果不给F赋初值(F = 1),则默认0开始依次+1
}
let grade = Grade.F
print("your score is \(grade.rawValue)") //your score is 0
// 依次给枚举变量赋值
enum Coin: Int {
case Penny = 1
case Nickel = 5
case Dime = 10
case Quarter = 25
}
var coin: Coin = .Quarter
print("your coin is \(coin.rawValue) cents") //your coin is 25 cents
//字符串类型的枚举变量
enum ProgrammingLanguage: String {
case Swift
case Java
case Ruby
case ObjectiveC = "Objective-C"
}
var language: ProgrammingLanguage = .Swift //默认可转化成字符串类型
print("My favorite language is \(language)") //My favorite language is Swift
- 关联值(Associate Value)
// Associate Value 跟 Raw Value 互斥
//传入单个数据类型
enum ATMStatus {
case Success(Int)
case Error(String)
}
var balance = 100
func withDraw(amout: Int) -> ATMStatus {
if balance >= amout {
balance -= amout
return .Success(balance)
}else {
return .Error("Not enough money")
}
}
let result = withDraw(amout: 20)
switch result {
case let .Success(newBalance):
print("\(newBalance) Yuan left in your acount.")
case let .Error(error):
print(error)
}
//传入多个数据类型
enum Shape {
case Square(long: Double)
case Rectangle(height: Double, width: Double)
case Circle(radius: Double)
}
func area(shape: Shape) -> Double {
switch shape {
case let .Circle(radius: r):
return Double.pi * r * r
case let .Rectangle(height: h, width: w):
return h * w
case let .Square(long: l):
return l * l
}
}
area(shape: .Circle(radius: 3))
area(shape: .Rectangle(height: 2, width: 3))
area(shape: .Square(long: 3))
- 可选型就是枚举类型
var website: Optional<String> = Optional.some("imooc.com")
website = .none
website = "imooc.com"
website = nil
switch website {
case let .some(website):
print("The website is \(website)")
case .none:
print("No website")
}
if let website = website {
print("The website is \(website)")
} else {
print("No website")
}
- 枚举递归
//1.定义递归枚举类型,加indirect关键字
indirect enum ArithmeticExpression {
case Number(Int)
case Addition(ArithmeticExpression, ArithmeticExpression)
case Mutiplication(ArithmeticExpression, ArithmeticExpression)
}
//(5+4)*2
//2.创建枚举型变量
let five: ArithmeticExpression = .Number(5)
let four: ArithmeticExpression = .Number(4)
let sum :ArithmeticExpression = .Addition(five, four)
let two: ArithmeticExpression = .Number(2)
let product:ArithmeticExpression = .Mutiplication(sum, two)
//3.创建函数switch每一个枚举变量,设定具体的用法
//递归枚举也要创建相应的递归函数
func evalute(expression: ArithmeticExpression) -> Int {
switch expression {
case let .Number(num):
return num
case let .Addition(left, right):
return evalute(expression: left) + evalute(expression: right)
case let .Mutiplication(left, right):
return evalute(expression: left) * evalute(expression: right)
}
}
//4.调用函数,传入变量
evalute(expression: product)
evalute(expression: .Number(5))