Lesson 13 of 15
Enums
Enums
Enums define a type with a fixed set of cases:
enum Direction {
case north
case south
case east
case west
}
let dir = Direction.north
Raw Values
Enums can have raw values of any type:
enum Planet: String {
case mercury = "Mercury"
case venus = "Venus"
case earth = "Earth"
}
print(Planet.earth.rawValue) // Earth
Switching on Enums
switch dir {
case .north:
print("Going north")
case .south:
print("Going south")
default:
print("Going elsewhere")
}
Your Task
Define an enum Planet with String raw values for mercury, venus, earth, mars. Then write a function describe that takes a Planet and returns a string like "Earth is a planet.".
JS Transpiler loading...
Loading...
Click "Run" to execute your code.