Chapter 3 Exercises — SwiftUI Fundamentals
These exercises build on the Chapter 3 project. Do them on a separate Git branch so your guided project stays identical to the reference checkpoint:
First commit and tag the guided work using the workbook's Development Checkpoint. Confirm git status --short prints nothing, then create the branch:
git switch -c challenge-03
When you are finished (whether or not you solved everything), commit your challenge edits (including unfinished work), then return to the main line before starting Chapter 4:
Uncommitted edits can follow you across branches. Save them before switching; if nothing changed, skip add and commit. The status command should print nothing:
git add -A
git commit -m "Challenge 03: Save practice work"
git status --short
git switch main
Solutions are in the Chapter 3 Answers document. Try each hint in order and only read the next one when you are stuck.
Challenge 1 — Add a roll-settings strip
Goal: preview the controls that later chapters add by showing a static row of three labels under the caption.
Requirements:
- Change only
ContentView.swift. - Add a row of three labels between the caption ("Tap Roll Dice to begin") and the second
Spacer(). - The labels read "D6", "1 die" and "Modifier +0", in that order, side by side, with 12 points between them.
- Each label uses the headline text style.
- Each label has 8 points of padding on every side.
- Each label has a pale orange background,
Color.orange.opacity(0.2), that covers the padding as well as the text.
Acceptance criteria:
- The project builds with no errors or warnings.
- The canvas and the Simulator show the three labels in a row under the caption.
- Each orange area is visibly larger than its text (the padding is inside the orange).
- The title, die display, caption and button look exactly as before, and the button still prints "Roll Dice tapped".
Hint 1
Think about the layout first. Three things side by side means a horizontal container, and "12 points between them" is a property of that container, not of each label. Where in the VStack does the new container go, relative to the caption and the Spacer()?
Hint 2
Each label is a Text with three modifiers: one for the font, one for the padding, and one for the background. You have used .font(.headline)-style text styles and .padding() already; .padding also accepts a number of points. The background modifier is the one from the "Modifier order matters" demonstration, .background(...).
Hint 3
The order of the last two modifiers decides whether the orange covers the padding. Re-read the first demonstration snippet in "Modifier order matters": the padding must already be part of the view when the background wraps it. Your container line looks like HStack(spacing: 12) {, and each of the three labels has the same three modifier lines with only the text changing.
Challenge 2 — Draw the tree
Goal: practice reading code as a view hierarchy.
Requirements:
- Without looking at the hierarchy diagram in Chapter 3, write the view tree of your finished Challenge 1
bodyas indented text. - Include every view: containers, spacers, shapes, texts, images and the button.
- Mark each view as container (it holds child views) or leaf (it does not).
- Do not include modifiers as separate nodes; you may note important ones in parentheses, such as "(spacing 12)".
Acceptance criteria:
- Every view in
bodyappears exactly once. - Each view is at the correct depth and in the same order as in the code.
Hint 1
Start at the top: what single view does body return? Everything else is inside it.
Hint 2
Read only the lines of your code that start a view (VStack, HStack, ZStack, Image, Text, Circle, Spacer, Button) and ignore lines that start with a dot. The indentation of those lines tells you their depth.
Hint 3
You should end up with three containers below ContentView that hold children (the main vertical stack and two horizontal stacks), plus one layered stack. Count your leaves: the finished Challenge 1 screen has two images-or-texts in the title row, a circle and a text in the die display, the caption, three labels in the strip, two spacers and the button.
Extra Practice
Exercise 1 — Predict the layout
Without running it, describe what this view looks like: where each piece sits on the screen and how they are arranged.
VStack {
Spacer()
HStack {
Text("D4")
Spacer()
Text("D20")
}
}
.padding()
Then type it into a temporary #Preview (or a branch copy of ContentView) and check.
Exercise 2 — Spot the bug
A developer wants a large orange "Roll Dice" button, but the project no longer builds. Find the problem.
Button("Roll Dice") {
print("Roll Dice tapped")
.font(.title2)
.buttonStyle(.borderedProminent)
.controlSize(.large)
.tint(.orange)
}
What will the compiler say, approximately, and how do you fix it?
Exercise 3 — Spot the bug
This die display shows a huge orange circle that fills most of the screen instead of a 160-point disc. Why?
ZStack {
Circle()
.fill(Color.orange.opacity(0.2))
Text("6")
.font(.system(size: 72, weight: .bold))
.frame(width: 160, height: 160)
}
Exercise 4 — Small modification
On your challenge branch, change the title row so the icon appears to the right of the "Dice Foundry" title, and make the icon use the same .largeTitle font as the text. Describe which lines you moved and which line you changed.
Exercise 5 — Predict the colors
What color is each piece of text in this snippet, and why?
VStack {
Text("Dice")
Text("Foundry")
.foregroundStyle(.orange)
}
.foregroundStyle(.secondary)