ios_catalogue1-6 Exercise 2: Immediate Year Error, Leading Spaces ================================================================== 1. Year error immediately: errors are shown when `showErrors && draft.yearError`. Show it as soon as the value is out of range by dropping the showErrors condition for that footer: } footer: { if draft.yearError { Text("Enter a four-digit year").foregroundStyle(.red) } } draft.yearError already returns false for an empty box and true for a value outside 1000...(this year + 1), so no change to ItemDraft is needed. The side effect: while typing "2", "20", "202" the value is out of range, so the message flashes until the fourth digit. A friendlier rule shows it only once four digits are entered, or after Save. That is a UX choice, not a code difficulty: change the condition to draft.yearText.count == 4 && draft.yearError (and keep the after-Save behaviour: showErrors && draft.yearError). 2. Ignore leading spaces in the title as the user types: use the same technique as the year filter, in ItemFormView: .onChange(of: draft.title) { _, newValue in let trimmed = String(newValue.drop(while: \.isWhitespace)) if trimmed != newValue { draft.title = trimmed } } Do not trim trailing spaces live: the user could never type a space between two words. save() still trims both ends as a final safety net. Where each belongs: - The RULE "a year must be 1000 to next year" belongs in ItemDraft (plain Swift, testable in Chapter 10). - WHEN to show the message, and live character filtering while typing, belong in the view (they are about the interaction). - Neither belongs in the repository: it should receive clean, valid values and only store them.