//枚举的基本用法
enum Direction {
case north
case south
case east
case west
}
//上面等价于下面的写法
//enum Direction {
// case north, south, east, west
//}
var dir = Direction.west
dir = Direction.east
dir = .south
print(dir) //north
switch dir {
case .north:
print("north")
case .south:
print("south")
case .east:
print("east")
case .west:
print("west")
}
//关联值 (Associated Values)
//有时会将枚举的成员值跟其他类型的关联存储在一起,会非常有用
enum Score {
case points(Int)
case grade(Character)
}
var score = Score.points(96)
score = .grade("A")
switch score {
case let .points(i):
print(i, "points")
case let .grade(i):
print("grade", i)
} //grade A
enum Date {
case digit(year: Int, month: Int, day: Int)
case string(String)
}
var date = Date.digit(year: 2011, month: 9, day: 10)
date = .string("2011-09-10")
switch date {
case .digit(let year, let month, let day):
print(year, month, day)
case let .string(value): //这里也可以写成 case .string(let value): //let也可以改成使用var
print(value)
}
//举例
enum Password {
case number(Int, Int, Int, Int)
case gesture(String)
}
var pwd = Password.number(3, 5, 7, 8)
pwd = .gesture("12369")
switch pwd {
case let .number(n1, n2, n3, n4):
print("number is", n1, n2, n3, n4)
case let .gesture(str):
print("gesture is", str)
}
//原始值(Raw Values)
//枚举成员可以使用“相同类型“的默认值预先关联,这个默认值叫做:原始值
enum PokerSuit : Character { //这里Character 是枚举类型,不是继承
case spade = "♠️" //这里的黑桃是枚举的原始值
case heart = "♥️"
case diamond = "♦️"
case club = "♣️"
}
var suit = PokerSuit.spade
print(suit) //spade
print(suit.rawValue) //♠️ rawValue可以获取到枚举类型的原始值
print(PokerSuit.club.rawValue) //♣️
enum Grade : String {
case perfect = "A"
case great = "B"
case good = "C"
case bad = "D"
}
print(Grade.perfect.rawValue) //A
print(Grade.great.rawValue) //B
print(Grade.good.rawValue) //C
print(Grade.bad.rawValue) //D
//隐式原始值(Implicitly Assigned Raw Values)
//如果枚举的原始值类型是Int,String,Swift会自动分配原始值
enum Direction2 : String {
case north = "north"
case south = "south"
case east = "east"
case west = "west"
}
//上面的写法等价于
enum Direction3 : String {
case north, south, east, west
}
print(Direction3.north) //north
print(Direction3.north.rawValue) //north
enum Season : Int {
case spring, summer, autumn, winter
}
print(Season.spring.rawValue) //0
print(Season.summer.rawValue) //1
print(Season.autumn.rawValue) //2
print(Season.winter.rawValue) //3
enum Season1 :Int {
case spring = 1, summer, autumn = 4, winter
}
//如果自己指定过原始值,那么没有指定过原始值的会再指定过的原始值基础上递增
print(Season1.spring.rawValue) //1
print(Season1.summer.rawValue) //2
print(Season1.autumn.rawValue) //4
print(Season1.winter.rawValue) //5
//枚举递归 (Recursive Enumeration)
//递归枚举必须要加indirect
indirect enum ArithExpr {
case number(Int)
case sum(ArithExpr, ArithExpr)
case difference(ArithExpr, ArithExpr)
}
//上面的写法也可以换成如下写法
enum AirthExpr1 {
case number(Int)
indirect case sum(AirthExpr1, AirthExpr1)
indirect case difference(AirthExpr1, AirthExpr1)
}
let five = AirthExpr1.number(5)
let four = AirthExpr1.number(4)
let two = AirthExpr1.number(2)
let sum = AirthExpr1.sum(five, four)
let difference = AirthExpr1.difference(sum, two)
func calculate(_ expr: AirthExpr1) -> Int {
switch expr {
case let .number(value):
return value
case let .sum(left, right):
return calculate(left) + calculate(right)
case let .difference(left, right):
return calculate(left) - calculate(right)
}
}
print(calculate(difference)) //7
//MemoryLayout
//可以使用MemoryLayout获取数据类型占用的内存大小
var age = 10
MemoryLayout<Int>.size //Int类型占用8字节
MemoryLayout<Int>.stride //8
MemoryLayout<Int>.alignment //内存对其按照8字节对齐
MemoryLayout.size(ofValue: age) //8
MemoryLayout.stride(ofValue: age) //8
MemoryLayout.alignment(ofValue: age) //8
enum Password1 {
case number(Int, Int, Int, Int)
case other
}
var pwd1 = Password1.number(5, 6, 4, 7)
MemoryLayout.size(ofValue: pwd1) //33
pwd1 = .other
MemoryLayout.size(ofValue: pwd1) //33
MemoryLayout<Password1>.stride // 40, 系统分配占用的内存大小,存33字节,按照8个字节对齐,所以是40个字节
MemoryLayout<Password1>.size // 33, 实际使用占用内存大小 (其中number 4个Int 4*8=32,other占1个,32+1=33)
MemoryLayout<Password1>.alignment //8, 对齐参数
enum Season2 {
//0 1 2 3
case spring, summber, autumn, winter
}
var s = Season2.spring
MemoryLayout<Season2>.size //1, 1个字节就可以区分4种类型
MemoryLayout<Season2>.stride //1
MemoryLayout<Season2>.alignment //1
enum Season3:String {
//0 1 2 3
case spring, summber, autumn, winter
}
var s3 = Season3.spring
MemoryLayout<Season3>.size //1, 1个字节就可以区分4种类型
MemoryLayout<Season3>.stride //1
MemoryLayout<Season3>.alignment //1
//关联值 和 原始值 区别
/**
case number(Int, Int, Int, Int)
- 关联值的数据是从外面传入的,所以要提前申请好内存
case spring="a"
- 原始值的值时固定的,并不会占用枚举变量的内存,当要去取值当时候,获取rawValue才会去对应地址获取原始值
*/
enum TestEnum: Int {
case test1=1, test2, test3
}
var t = TestEnum.test1
t = .test2
t = .test3
print(MemoryLayout<TestEnum>.size) //1,
print(MemoryLayout<TestEnum>.stride) //1
print(MemoryLayout<TestEnum>.alignment) //1
print(MemoryLayout.size(ofValue: TestEnum.test1))//1, 枚举变量的内存还是用1个字节表示,原始值并没有存到变量的内存中