使用Swift 枚举,您可以省略名称enum,在只能使用该类型的值的情况下,。
所以当给出枚举(Swift/Kotlin)
enum (class) CompassPoint {
case north
case south
case east
case west
}
Run Code Online (Sandbox Code Playgroud)
Swift 在创建新变量时只需要枚举名称:
// type unclear, enum name needed
var directionToHead = CompassPoint.west
// type clear, enum name can be dropped
directionToHead = .east
// type clear, enum name can be dropped
switch directionToHead {
case .north:
print("Lots of planets have a north")
case .south:
print("Watch out for penguins")
case .east:
print("Where the sun rises")
case .west:
print("Where the skies are …Run Code Online (Sandbox Code Playgroud)