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.
Swift Fundamentals
Model independent values and finite states with Swift structures and enumerations.
Before you begin
Learning stage
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.
Learning stage
The struct models lesson data, while the enum models every valid loading state.
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])Learning stage
These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.
Learning stage
Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.
Learning stage
Work through these activities in a small Xcode project. No embedded playground is required on this site.
Activity 1
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
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.
Learning stage
Answer each question before revealing the explanation.
QUESTION 1
Structures and enumerations
Assignment and parameter passing work with independent values rather than shared class identity.
QUESTION 2
It can make invalid combinations impossible by representing exactly one valid case at a time.
Learning stage
Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.
Short answer: Each variable has its own value, so changing one value does not silently mutate another copy.
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
Learning stage
Value types help Swift programs communicate state explicitly. Structures group related values, while enumerations define the complete set of valid alternatives.