Polish and Errors
Personal Catalogue: iOS (Swift & SwiftUI)
Course 1 ยท Chapter 9 ยท Appearance, Accessibility and Errors
The app now does everything the spec asks, but three kinds of shortcut are still in it. Failures are printed to a console nobody sees. Nothing has been checked in dark mode, with VoiceOver, or with
the text size turned up. This chapter pays all three off.
Nothing here was compiled or run (no Mac or Xcode). My search found third-party accessibility guides, not Apple's pages, and nothing on
ViewThatFits, dark-mode colour rules or
accessible error alerts; those are from memory.
Part 1: Light and Dark Appearance
SwiftUI adapts on its own as long as colours are semantic: .primary, .secondary, .accentColor, Color(.secondarySystemBackground), and the system
named colours like .red and .orange, which have light and dark variants. The risk is a fixed colour (Color(red:green:blue:), a hex value, .black,
.white used for a background). Audit what the app uses:
| Where | Colour | Verdict |
|---|---|---|
FilterChip text on a selected chip | Color.white on Color.accentColor | Probably fine, but a fixed white on a variable accent: check contrast in both modes |
FilterChip unselected | .primary on secondarySystemBackground | Semantic: adapts |
TagChip background | .tint.opacity(0.15) | Adapts, but low contrast is possible; check it |
| Form errors, duplicate warning | .red, .orange | System colours: adapt. Orange text on a light background can be weak: check |
| Row text | .secondary, .tint | Semantic: adapts |
Verify instead of assuming: add a dark and a large-text preview next to the normal one for each screen. The scheme and the text size are both just environment values you can override in a preview:
A preview is a quick check, not the final one: also toggle Dark Appearance and the text size on a simulator or device (the Xcode Environment Overrides button, or the Settings app), because some problems only
show on the real screen. Fixing a weak spot is usually one line, for example choosing a darker text colour for the orange warning with .foregroundStyle(.orange) replaced by .foregroundStyle(.primary)
next to the orange icon.
Part 2: VoiceOver
The sources agree on the main tools: a custom control needs a label; a group of views that mean one thing should be one VoiceOver element, with .accessibilityElement(children: .combine); and a real
Button gets focus handling and traits for free, which is why Chapter 4's chip is a Button and not an onTapGesture. Three changes:
Two things are decorative and should be hidden from VoiceOver if you add them (the sources mention this for icons): an icon that only repeats adjacent text gets
.accessibilityHidden(true). A row's tap action is the navigation link itself, which VoiceOver already announces as a button, so do not add a hint for that; hints that restate the obvious
make the app slower to use, not better.
The honest way to check is to use it: turn VoiceOver on in a simulator or device (or open Xcode's Accessibility Inspector) and move through the list, the chips, the form and the delete dialog with your eyes shut or the screen dimmed. The sources are explicit that snapshot tests do not verify VoiceOver or Dynamic Type; only you using it does.
Part 3: Dynamic Type
Every piece of text in this app uses a text style (.headline, .subheadline, .caption), which scales with the user's size setting automatically. A hard-coded
.font(.system(size: 14)) would not, as one source warns; the app has none. What can still break is the layout around scaled text:
- Chip rows scroll horizontally, so large text only makes chips wider; nothing is cut off, but fewer are visible at once.
- Row text: the title has
.lineLimit(2), which at the largest sizes truncates long titles. Remove the limit (or raise it) for accessibility sizes, using@Environment(\.dynamicTypeSize)anddynamicTypeSize.isAccessibilitySize(a property I recall from memory; check Xcode accepts it). - The year field and fixed-size icons that sit beside text should scale with it: use
@ScaledMetricfor any number that is a size, as the sources suggest for padding and image sizes. - Side-by-side layouts such as the chip's label and remove button can overflow;
ViewThatFits(not covered by my search, so from memory) lets a view choose a vertical layout when the horizontal one does not fit.
Part 4: Real Error Handling
Chapters 3 to 6 each ended a catch with print(...) and a comment promising this chapter. Two situations, with different answers:
- Save fails in the form. The form is still on screen, so show an alert there and keep the sheet open so nothing the user typed is lost.
- Delete fails after the detail screen has been dismissed (Chapter 5's ordering). The screen that started it is gone, so the message needs somewhere else to live: a small app-wide error centre that the root view turns into an alert.
The form's alert is plain local state. Add to ItemFormView:
A subtlety for save(): the repository inserts a new item before the save can fail, so a failed Save leaves a half-added object in the context. If the user fixes the problem and taps Save
again, they could create a second one. The safe repository behaviour on failure is to undo what it inserted; the repository is the place for that, not the form. I did not rewrite it here, so Exercise 3 asks you to.
The error centre, for failures the current screen cannot show. Create ErrorCenter.swift:
@Environment(ErrorCenter.self) and .environment(errors) are the iOS 17 way to share an @Observable object (from memory; the search did not cover them). Any view that
forgets to receive the object crashes at runtime, so previews that show DetailView must also supply .environment(ErrorCenter()). The same centre is where Chapter 10's import errors will go.
Not-found states. The Android course needed Loading and NotFound states because its detail screen loaded by id. This app passes the object, so those states do not exist; the nearest equivalent
is Chapter 5's guard against showing a deleted model. There is also no "Loading" for lists, because @Query returns synchronously from the local store.
Challenges
Run an audit: Dark Appearance, the largest accessibility text size, and VoiceOver (or the Accessibility Inspector) on the list, detail and form. Write down at least three real findings (something unclear, cut off, or low-contrast), the change you made for each, and the iOS version.
๐ View solutionAdd the form's alert and the error centre. To test the form alert, make the repository's save temporarily throw. Record what the user sees, whether the sheet stays open, and
whether their typing survives.
Fix the half-inserted item problem: change CatalogueRepository.save so that when saving fails, anything it inserted is removed again and the error is rethrown. Explain why
retrying Save must not create two items.
Chapter 9 Quick Reference
- Semantic colours only (
.primary,.secondary,.accentColor, system backgrounds); preview with.preferredColorScheme(.dark) - VoiceOver:
.accessibilityElement(children: .combine),.accessibilityLabel,.accessibilityHint(sparingly),.accessibilityHidden(true)for decoration - Use
Button, notonTapGesture, for tappable things - Dynamic Type: text styles scale; fix layouts with
@ScaledMetric,ViewThatFits, and lifting line limits at accessibility sizes; preview with.environment(\.dynamicTypeSize, .accessibility3) - A failure on a visible screen: an
.alertthere, and keep the screen open. A failure after dismissal: an@Observableerror centre in the environment, alerted from the root - The repository should undo its own inserts when a save fails
.accessibilityElement(children: .combine) merges children into one VoiceOver element; that the main VoiceOver
modifiers are label, hint, value and action; that Button gives focus handling and traits for free; that system text styles scale automatically and a fixed .font(.system(size:)) does not;
that @ScaledMetric scales a number with Dynamic Type; and that snapshot tests do not verify Dynamic Type or VoiceOver.Not checked, from general knowledge: that the named system colours adapt (and the contrast judgements in the table, which you must test yourself);
ViewThatFits;
dynamicTypeSize.isAccessibilitySize; .accessibilityHidden; the .alert form with an item-less binding and a message closure;
@Environment(ErrorCenter.self) and .environment(errors) for an @Observable class; error.localizedDescription being readable for SwiftData errors (it often is
not); whether an alert can appear at the root while the detail screen is mid-dismissal; and all code, none of which was compiled. I did not consult Apple's accessibility pages.
fileExporter and fileImporter (reporting failures through this chapter's error
centre), tests for the repository and the draft, and a roadmap with barcode scanning, compared with the Android course. The course PDF is built at the end.