Skip to content
Learn Netverks

Lesson

Step 16/36 44% through track

enums-swift

Enums

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Enums: the syntax, patterns, and safety habits you need before advancing in Swift.

Teams still ship Enums in Swift codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Enums in contexts like: iPhone/iPad/Mac apps, server-side Swift (niche), and Apple toolchain projects.

Write Swift in main.swift with print(), click Run on server—the dev runner swiftc compiles and runs the binary (requires Swift toolchain, typically macOS; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Swift enums are unusually powerful—raw values, associated values, and methods make them sum types comparable to sealed classes in Kotlin or discriminated unions in Rust.

Basic and raw-value enums

enum Direction { case north, south, east, west }

enum Planet: Int {
    case mercury = 1, venus, earth
}

Associated values

enum Result {
    case success(String)
    case failure(code: Int, message: String)
}

Important interview questions and answers

  1. Q: Enum vs struct?
    A: Enums model fixed alternatives with optional payloads; structs model structured data with fields.
  2. Q: CaseIterable?
    A: Protocol auto-synthesizes allCases for enums without associated values—handy for pickers.

Self-check

  1. What are associated values?
  2. Can enums have methods?

Tip: Associated values make enums sum types—similar to sealed classes in Kotlin.

Interview prep

Associated values?

Each case can carry typed payloads—sum types.

Enum methods?

Yes—enums can include methods and protocol conformance.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Associated values?
  • CaseIterable?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump