Skip to content
Prepare to Grow

Swift Fundamentals

Structures, Enumerations, and Value Semantics

Model independent values and finite states with Swift structures and enumerations.

Beginner4 min read2 guided activities

Before you begin

What you will understand

  • Declare structures and enumerations.
  • Explain value semantics.
  • Use enum associated values.
  • Model app state without invalid combinations.
01

Learning stage

Concept Explanation

Structures and enumerations are value types. When a value is assigned to another variable or passed into a function, the receiving context gets its own value relationship. Later mutations remain local unless they are communicated deliberately.

Enumerations define a finite set of valid cases and can attach associated data to individual cases. Structures group related properties and behavior. Together they make many Swift models predictable without shared object identity.

  • struct groups related value data and behavior.
  • enum represents one case from a finite set.
  • Associated values carry information specific to one enum case.
  • Value semantics keep changes local until explicitly passed elsewhere.
02

Learning stage

Syntax & Examples

The struct models lesson data, while the enum models every valid loading state.

LessonModels.swiftswift
struct Lesson {
  let title: String
  var isComplete = false

  mutating func markComplete() {
      isComplete = true
  }
}

enum LoadState {
  case idle
  case loading
  case loaded([Lesson])
  case failed(message: String)
}

var lesson = Lesson(title: "Optionals")
lesson.markComplete()
let state = LoadState.loaded([lesson])
The model can represent only the declared loading states, and each loaded state carries lessons.
03

Learning stage

Real-World Usage

These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.

  • Use structures for screen models, coordinates, configuration, and independent domain values.
  • Use enums for navigation destinations, request states, filters, and domain choices.
  • Use associated values when each state requires different contextual information.
04

Learning stage

Common Mistakes

Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.

  • Using several independent Booleans to represent mutually exclusive states.
  • Forgetting mutating on a struct method that changes stored properties.
  • Assuming copied structures continue sharing later mutations.
  • Adding a default switch case that hides newly introduced enum cases.
05

Learning stage

Practice Exercises

Work through these activities in a small Xcode project. No embedded playground is required on this site.

Activity 1

Model a download

Create an enum for notStarted, downloading(progress:), completed(fileURL:), and failed(message:) states.

Hint: Associated values should carry only the information meaningful to each state.

Activity 2

Predict value semantics

Copy a mutable Lesson struct into a second variable, change one copy, and predict the value of both before running it.

Hint: Each variable has its own value after assignment.

06

Learning stage

Knowledge Check

Answer each question before revealing the explanation.

QUESTION 1

Which Swift custom types have value semantics?

  1. A.Structures and enumerations
  2. B.Classes only
  3. C.Protocols only
  4. D.All types use reference semantics
Reveal answer

Structures and enumerations

Assignment and parameter passing work with independent values rather than shared class identity.

QUESTION 2

Why can an enum be safer than several state Booleans?

Reveal answer

It can make invalid combinations impossible by representing exactly one valid case at a time.

07

Learning stage

Interview Preparation

Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.

What does value semantics mean in Swift?

Short answer: Each variable has its own value, so changing one value does not silently mutate another copy.

Explore the detailed answer

Structures and enumerations use value semantics. Assignment and parameter passing preserve logical independence, which reduces hidden shared mutation and helps code remain understandable from local context.

Possible follow-ups

  • • Why are value types useful for app state?
  • • How do enum associated values work?
08

Learning stage

Key Takeaways

Value types help Swift programs communicate state explicitly. Structures group related values, while enumerations define the complete set of valid alternatives.

  • Structures and enums are value types.
  • Value semantics reduce hidden shared mutation.
  • Enums can carry associated values.
  • Exhaustive switching helps the compiler protect state handling.