Swift中的枚举比OC中的枚举更强大。
一. 枚举的基本用法

二. 关联值(Associated Values)
有时将枚举的成员值跟其他类型的值关联存储在一起,会非常有用

关联值举例:

三. 原始值(Raw Values)
枚举成员可以使用相同类型的默认值预先对应,这个默认值叫做:原始值

上面的 : Character和 : String并不是什么继承,而是代表枚举的原始值是什么类型。
枚举可以根据rawValue来给枚举赋值,但是没有什么所谓的初始化器,比如:
let season = Season(rawValue: 1) //夏天
注意:原始值不占用枚举变量的内存
- 隐式原始值(Implicitly Assigned Raw Values)
如果枚举的原始值类型是Int、String,Swift会自动分配原始值
如果原始值类型是String,那么原始值就是枚举成员的名字,比如下面两种写法等价:

如果原始值类型是Int,那么原始值从0开始,并且递增,如下:

四. 递归枚举(Recursive Enumeration)
一个枚举成员里面又用到了这个枚举,那么这种枚举就叫递归枚举,递归枚举必须要加上indirect关键字,否则编译器会报错。
下面是一个做算数运算的递归枚举,枚举成员分别是数字、加法、减法:

计算的是:(5 + 4) - 2 = 7。
五. MemoryLayout
可以使用MemoryLayout获取数据类型占用的内存大小

var pwd = Password.numer(9, 8, 6, 4)
pwd = .other
MemoryLayout.size(ofValue: pwd) // 至少需要33字节
MemoryLayout.stride(ofValue: pwd) // 实际系统分配40字节
MemoryLayout.alignment(ofValue: pwd) // 内存对齐8
上面的枚举,int占用8字节,case other只需1字节,4 x 8 + 1 = 33字节,又因为对齐参数是8,所以枚举变量至少需要33字节,实际系统分配的是40字节。
可能你还有疑问,既然case number(Int, Int, Int, Int)占用32字节,case other占用1字节,那存储other的时候直接用那32个字节不就好了吗?如果真的是other用那32个字节中的一个字节,那么你就没法分辨到底是other还是number了,所以必须要有一个字节用来分辨是other还是number,所以必须是32 + 1 = 33字节。
- 思考下面枚举变量的内存布局
enum Season {
// 就相当于存 0 1 2 3 就能分辨出是哪个枚举值了
case spring, summer, autumn, winter
}
var s = Season.spring //相当于存 0
var s1 = Season.summer //相当于存 1
var s2 = Season.autumn //相当于存 2
MemoryLayout.size(ofValue: s) // 1字节
MemoryLayout.stride(ofValue: s1) // 1字节
MemoryLayout.alignment(ofValue: s2) // 1字节
上面枚举变量占用一个字节,这也很容易理解,对于Season枚举,最简单的方法就是在内存中存0 1 2 3就能表明是哪个枚举成员了,所以只需要一个字节就可以。
如果加个Int原始值呢?
enum Season : Int {
// 就相当于存 0 1 2 3 就能分辨出是哪个枚举值了
case spring = 6, summer, autumn, winter
}
var s = Season.spring //相当于存 0
var s1 = Season.summer //相当于存 1
var s2 = Season.autumn //相当于存 2
MemoryLayout.size(ofValue: s) // 1字节
MemoryLayout.stride(ofValue: s1) // 1字节
MemoryLayout.alignment(ofValue: s2) // 1字节
由于原始值不占用枚举变量的内存,所以上面枚举变量还是占用一字节。
关联值和原始值的区别:
① 关联值是和枚举变量关联起来的,占用枚举变量的内存,原始值不占用枚举变量的内存。
② 关联值是不固定的,要根据外面传进来的值决定,原始值是固定死的,既然是固定死的,那么就没必要存储在枚举变量内存里面。
可能你会想,既然原始值不占用枚举变量的内存,那么原始值存储在哪里呢?其实完全没必要纠结原始值存储在哪里,因为原始值完全可以不存嘛,上面枚举的rawValue伪代码,如下:
func rawValue() -> Int {
if self == 6 return 6
if self == 7 return 7
if self == 8 return 8
if self == 9 return 9
}
网友评论