- 可以将自定义的枚举类型遵循
CaseIterable
来允许枚举被遍历. -
Swift
会暴露一个包含对应枚举类型所有情况的集合名为allCases
e.g.
enum CompassPoint: CaseIterable {
case north
case south
case east
case west
}
let numberOfDirections = CompassPoint.allCases.count
print("\(numberOfDirections) directions")
for direction in CompassPoint.allCases {
print("direction: \(direction)")
}
输出结果:
4 directions
direction: north
direction: south
direction: east
direction: west
网友评论