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.

A correction first, and what was run
The duplicate warning is already built. The outline gives this chapter "Duplicates & Polish", but Chapter 8 absorbed the duplicate check because it belongs with search ("do I own this?"). I also wrote in Chapter 8 that this chapter would cover backup; that was wrong. Backup is Chapter 10, with the tests, and I have corrected Chapter 8's "Coming next" note. The takeaway for the course plan: check the outline row before announcing the next chapter.
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:

WhereColourVerdict
FilterChip text on a selected chipColor.white on Color.accentColorProbably fine, but a fixed white on a variable accent: check contrast in both modes
FilterChip unselected.primary on secondarySystemBackgroundSemantic: adapts
TagChip background.tint.opacity(0.15)Adapts, but low contrast is possible; check it
Form errors, duplicate warning.red, .orangeSystem colours: adapt. Orange text on a light background can be weak: check
Row text.secondary, .tintSemantic: 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:

#Preview("Dark") { ListView() .modelContainer(PreviewData.container) .preferredColorScheme(.dark) } #Preview("Large text") { ListView() .modelContainer(PreviewData.container) .environment(\.dynamicTypeSize, .accessibility3) }

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:

// ItemRow: one spoken sentence per row instead of three separate fragments. VStack(alignment: .leading, spacing: 4) { /* title, byline, type and tags as before */ } .padding(.vertical, 2) .accessibilityElement(children: .combine) // FilterChip: say what tapping does. .accessibilityAddTraits(isSelected ? .isSelected : []) .accessibilityHint("Filters the list") // DetailView toolbar: icon-only buttons already carry their title as the label; confirm in the Accessibility Inspector.

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) and dynamicTypeSize.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 @ScaledMetric for 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:

@State private var saveError: String? // in save(): replace the print in the catch } catch { saveError = error.localizedDescription // the sheet stays open } // next to .toolbar on the Form .alert("Couldn't save", isPresented: Binding( get: { saveError != nil }, set: { if !$0 { saveError = nil } } )) { Button("OK", role: .cancel) { } } message: { Text(saveError ?? "") }

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:

import Observation @Observable final class ErrorCenter { var message: String? func report(_ error: Error, doing action: String) { message = "Couldn't \(action): \(error.localizedDescription)" } } // RootView: own it, share it, present it. @State private var errors = ErrorCenter() // on the root content: .environment(errors) .alert("Something went wrong", isPresented: Binding( get: { errors.message != nil }, set: { if !$0 { errors.message = nil } } )) { Button("OK", role: .cancel) { } } message: { Text(errors.message ?? "") } // DetailView: read it from the environment, and use it in deleteItem's catch. @Environment(ErrorCenter.self) private var errors ... } catch { errors.report(error, doing: "delete the item") }

@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.

Compare with the Android course
Personal Catalogue: Android (Kotlin & Compose) Chapter 9 handled errors with state in view models and a snackbar, and its accessibility work centred on TalkBack semantics and merged cards. The SwiftUI version has the same three jobs with fewer moving parts, because semantic colours and text styles are the default rather than something you set up. The price is the same on both platforms: the defaults get you most of the way, and the remaining problems only show when a person actually uses a screen reader or large text. Neither platform's tests will find them for you.

Challenges

Exercise 1

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 solution
Exercise 2

Add 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.

๐Ÿ“„ View solution
Exercise 3

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.

๐Ÿ“„ View solution

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, not onTapGesture, 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 .alert there, and keep the screen open. A failure after dismissal: an @Observable error centre in the environment, alerted from the root
  • The repository should undo its own inserts when a save fails
Not Yet Verified
Checked (one web search on 2026-11-19, third-party guides only): that .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.
Coming next
Personal Catalogue: iOS (Swift & SwiftUI) 10 is the last chapter: JSON backup and restore with 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.