Swift enum

up:: Swift ⚐

enum CompassPoint: String {
 case north
 case south
 case east
 case west
}

Shorthand

enum TemperatureUnit: String {
 case celsius, fahrenheit, kelvin
}

Id

enum FoodCategory: String, Identifiable {
    case italian = "Italian"
    case chinese = "Chinese"
    case indian = "Indian"
    case american = "American"
 
    var id: Self { self }
}

CaseIterable

This automatically adds the .allCases property, which is an array of all the cases.

enum Color: CaseIterable {
    case red, green, blue
}

This makes the enum iterable:

for color in Color.allCases {
    print("My favorite color is \(color).")
}