Thuta Learning
ExercisesMobile Developmentintermediate

Practice Set 1

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand Practice Set 1 with zero fear
  • Run the Xcode/SwiftUI code yourself
  • Apply this concept in a real project right away

Let's think about it this way for a second

This round is about practicing the Basic/Intermediate concepts you've already learned — telling View/@State/Modifier apart, tracking down errors in SwiftUI code, and using `@State` correctly, all hands-on. No single task should take you more than 5 minutes.

Let's connect this to a real-world scenario

Task 1: Explain the differing roles of 'View protocol', '@State', and 'Modifier' in one sentence each. Task 2: Write `var count = 0` (without `@State`) inside a View struct, and predict what happens when a Button tap runs `count += 1`. Task 3: Write an example of when you should use `List` instead of VStack+ForEach. Task 4: Explain the difference between `@State` and `@Binding` using an example of how a parent and child View communicate.

Let's walk through it together

text
# Task 2 - mutable property without @State
var count = 0   // NOT wrapped in @State!

Button("Increment") { count += 1 }

Result: this is a compile-time error — SwiftUI View is a
struct (immutable), so a plain `var` property cannot be
mutated from inside a closure. The compiler says
"Cannot assign to property: 'self' is immutable".
@State is required to make it mutable.

# Task 4 - @State vs @Binding
@State  = the View that OWNS the value (private, source of truth)
@Binding = a child View that needs to READ AND WRITE a value
           it doesn't own — the parent passes it in with $
You should see
You'll come away with a clear grasp of the View/@State/Modifier differences, what a missing-@State compile error looks like, and how to decide between @State and @Binding.

5-minute try-it

Write your own View and deliberately leave out `@State` to introduce a mutable-property bug — then read through Xcode's compile error message yourself.

A quick word of caution

You don't need to set up a physical device for this round — just focus on practicing with the Simulator (or the #Preview panel).

Easy traps

  • Getting confused and thinking View/@State/Modifier are 'all basically the same thing'
  • Skimming past a SwiftUI compile error message without checking which line it's actually pointing to

Now try it yourself

Write your own View and deliberately leave out `@State` to introduce a mutable-property bug — then read through Xcode's compile error message yourself.

You'll know it worked when: You'll come away with a clear grasp of the View/@State/Modifier differences, what a missing-@State compile error looks like, and how to decide between @State and @Binding.

Practice Set 1 | Thuta Learning