Thuta Learning
Swift
AdvancedProgrammingBeginner

Enumerations

SwiftLesson 2125 min

What you'll walk away with

  • Explain the type, ownership, and runtime behavior of Enumerations
  • Test nil, error, boundary, and concurrency cases
  • Write safe and testable Swift

Explain it simply

Enumerations, in plain language

Enum s 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.

Problem → Why → Solution

The problem — why this concept matters

Defining an enum is easier to maintain than hardcoding string statuses everywhere.

A common mistake

Assuming one sample output proves nil, ownership, actor isolation, and every error path.

Core concept — understand how it works

Use Enumerations to express stable capabilities and reusable algorithms. Prefer protocol-oriented design, constrained generics, and focused extensions to reduce coupling to concrete types, inheritance, and global state.

Mental model
  1. optional or fallible input
  2. pattern and error handling
  3. safe value or recovery

Explain the type, ownership, and runtime behavior of Enumerations

Core syntax or pattern

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")
}

Use Enumerations to express stable capabilities and reusable algorithms. Prefer protocol-oriented design, constrained generics, and focused extensions to reduce coupling to concrete types, inheritance, and global state.

Read the code step by step

  1. optional or fallible inputenum AppTheme {

    Enum s 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.

  2. pattern and error handlingcase .light:

    Use Enumerations to express stable capabilities and reusable algorithms. Prefer protocol-oriented design, constrained generics, and focused extensions to reduce coupling to concrete types, inheritance, and global state.

  3. safe value or recovery}

    Run and test an original Enumerations example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

Execution flow
  1. optional or fallible input
  2. pattern and error handling
  3. safe value or recovery

Success

Explain the type, ownership, and runtime behavior of Enumerations

Failure

Defining an enum is easier to maintain than hardcoding string statuses everywhere.

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

Real-world usage

How to apply it in a real project

Run and test an original Enumerations example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

Mini challenge

Build it yourself

Run and test an original Enumerations example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
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")
}

Expected: Explain the type, ownership, and runtime behavior of Enumerations

Show hint

Run and test an original Enumerations example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

Quick quiz 1 / 3

What best describes the core idea of Enumerations?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • optional or fallible input → pattern and error handling → safe value or recovery
  • Use Enumerations to express stable capabilities and reusable algorithms. Prefer protocol-oriented design, constrained generics, and focused extensions to reduce coupling to concrete types, inheritance, and global state.
  • Defining an enum is easier to maintain than hardcoding string statuses everywhere.
  • Run and test an original Enumerations example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

The Swift Programming Language — Swift.org

Easy traps

  • Defining an enum is easier to maintain than hardcoding string statuses everywhere.
  • Assuming one sample output proves nil, ownership, actor isolation, and every error path.
  • Bypassing compiler safety with force unwraps or unchecked shared mutable state.

Hands-on Exercise

Run and test an original Enumerations example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

You'll know it worked when: Use dark colors

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Enumerations | Thuta Learning