Stage 1 · Lesson 4 of 10
Structs versus classes
This is the most important Swift idea for . A is a . Assigning it or passing it to a function makes a copy, so changing the copy never affects the original.
A class is a reference type. Every variable points to the same object, so a change made through one is seen by all. Swift prefers structs for models, and SwiftUI views are structs too.
Methods that change a struct must be marked mutating. and strings are structs as well, and copying them is cheap thanks to copy-on-write (the real copy only happens when one side changes). Use a class when you need one shared object, such as an observable .
Common trapexpecting a copied struct to update the original.
A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock
Resources for this stage
Start with
- CourseFree100 Days of SwiftUI
Paul Hudson (Hacking with Swift) · Beginner
Days 1 to 15 teach Swift, with a checkpoint exercise every other day