enum Season {
case spring
case summer
case autumn
case winter
}
注意:Swift枚举在创建时未分配默认整数值。spring,summer,autumn和winter不等于隐式0,1,2和3。相反,这些值的类型是已经明确定义好的Season类型
var season = Season.spring
print(season) //spring
season = .summer
print(season) //summer
enum Color{
case red
case green
case yellow
}
let currentColor = Color.yellow
//因为Color是一个枚举,所以rememberedColor的值其实是currentColor的一个拷贝副本,而不是currentColor本身,它们是两个完全不同的实例
var rememberedColor = currentColor
rememberedColor = .green
//通过log证明currentColor和rememberedColor其中任何一个修改都不会影响另一个
print(currentColor) //yellow
print(rememberedColor) //green
在判断一个枚举类型的值时,switch语句必须穷举所有情况,忽略一个将无法通过编译
var season = Season.spring
switch season {
case .spring:
print("spring")
case .summer:
print("summer")
case .autumn:
print("autumn")
case .winter:
print("winter")
}
枚举遵循CaseIterable 协议
enum Season:CaseIterable {
case spring
case summer
case autumn
case winter
}
let count = Season.allCases.count
print(count) //4
for season in Season.allCases {
print(season)
}
//spring
//summer
//autumn
//winter
声明存储不同类型关联值的枚举成员(这个定义不提供任何Int或String类型的关联值) 一个成员值是(Int,Int,Int)类型关联值的num 一个成员值是(String,String)类型关联值的str
enum Code{
case num(Int,Int,Int)
case str(String,String)
}
创建了一个名为code的变量,并为Code.num和Code.str赋值
var code = Code.num(2, 3, 3)
code = .str("A", "B")
使用switch语句检查不同类型
switch code {
case .num(let num1, let num2, let num3):
print("\(num1),\(num2),\(num3)")
case .str(let str1, let str2):
print("\(str1),\(str2)")
}
//如果枚举的所有关联值都被提取为常量或者变量,则可以在前面放置单个var或let注释,以简洁起见
switch code {
case let .num(num1,num2,num3):
print("\(num1),\(num2),\(num3)")
case let .str(str1,str2):
print("\(str1),\(str2)")
}
enum Season:String {
case spring = "A"
case summer = "B"
case autumn = "C"
case winter = "D"
}
注意: 原始值和关联值是不同的。原始值是在定义枚举时被预先填充的值。对于一个特定的枚举成员,它的原始值始终不变。关联值是创建一个基于枚举成员的常量或变量时才设置的值,枚举成员的关联值可以变化。
enum Season:Int {
case spring = 1
case summer
case autumn
case winter
}
enum Season:String {
case spring
case summer
case autumn
case winter
}
print(Season.spring.rawValue)
rawValue
初始化一个枚举成员,返回值则是枚举成员或nil
可以通过这个来判断这个新枚举成员是否在枚举值中enum Season:Int {
case spring
case summer
case autumn
case winter
}
if let season = Season(rawValue: 5){
switch season{
case .spring:
print("spring")
case .summer:
print("summer")
case .autumn:
print("autumn")
case .winter:
print("winter")
}
}else{
print("凉凉")
}
indirect
来表示该成员可递归enum ArithmeticExpression {
case number(Int)
indirect case addition(ArithmeticExpression, ArithmeticExpression)
indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}
枚举之前写indirect来让整个枚举成员在需要时可以递归
indirect enum ArithmeticExpression {
case number(Int)
case addition(ArithmeticExpression, ArithmeticExpression)
case multiplication(ArithmeticExpression, ArithmeticExpression)
}
indirect enum ArithmeticExpression {
case number(Int)
case addition(ArithmeticExpression, ArithmeticExpression)
case multiplication(ArithmeticExpression, ArithmeticExpression)
}
func evaluate(_ expression:ArithmeticExpression) -> Int {
switch expression {
case let .number(value):
return value
case let .addition(value1, value2):
return evaluate(value1) + evaluate(value2)
case let .multiplication(value1, value2):
return evaluate(value1) - evaluate(value2)
}
}
let num = ArithmeticExpression.number(5)
print(evaluate(num)) // 打印 "5"
print(evaluate(ArithmeticExpression.addition(num, num))) // 打印 "10"
print(evaluate(ArithmeticExpression.multiplication(num, num))) // 打印 "0"