// *** 以前 ***
enum DirectionOld {
case north, south, east, west
// 列表靠手工维护,新增的 case 被遗漏
static let all: [DirectionOld] = [.north, .south, .east, .west]
}
// *** 在 4.2 版本中 ***
enum Direction: CaseIterable {
case north, south, east, west
}
print(Direction.allCases.count) // 4
for direction in Direction.allCases {
print(direction)
}
let random = Direction.allCases.randomElement()
// 编译器只为没有关联值的枚举生成 allCases;
// 被 @available 隐藏的 case 不会出现在其中