Thuta Learning
AdvancedProgrammingbeginner

Enumerations

Relax. We'll talk through this in plain words — no textbook voice.

Enums group together a related set of possible values. For things like app status, screen routes, payment state, direction, and theme mode, writing them as an enum is much safer than typing out random strings.

swift
enum AppTheme {
    case light
    case dark
    case system
}

let selectedTheme = AppTheme.dark

switch selectedTheme {
case .light:
    print("Use light colors")
case .dark:
    print("Use dark colors")
case .system:
    print("Follow device setting")
}

AppTheme enum defines three possible app theme options. switch handles every enum case, which cuts down on future bugs.

You should see
Use dark colors

Easy traps

  • Defining an enum is easier to maintain than hardcoding string statuses everywhere.
Enumerations | Thuta Learning