Displaying Human-Friendly Content
At a glance
| Item | Summary |
|---|---|
| Purpose | Explore Foundation formatters for dates, measurements, names, and numbers in a localized SwiftUI interface. |
| Architecture | A cross-platform catalog whose navigation root composes self-contained formatter demonstration views. |
| State style | Each feature view owns its formatter settings, often in a nested private FormatterState value. |
Project structure
├── Shared/
│ ├── FormattersApp.swift
│ ├── ContentView.swift
│ ├── Dates/*View.swift
│ ├── Names/*View.swift
│ ├── MeasurementsView.swift
│ └── NumbersView.swift
├── iOS/Utilities.swift
└── macOS/Utilities.swift
Almost all source is shared. Two same-signature utility functions adapt pasteboard writes to UIKit or AppKit at compile time.
Overall architecture
flowchart LR
App["FormattersApp"] --> Nav["ContentView navigation"]
Nav --> Dates["Dates views"]
Nav --> Measures["MeasurementsView"]
Nav --> Names["NamesView"]
Nav --> Numbers["NumbersView"]
Dates --> Foundation["Foundation formatter objects"]
Measures --> Foundation
Names --> Foundation
Numbers --> Foundation
Dates --> Pasteboard["platform Utilities.swift"]
Reference code
Shared/ContentView.swift:10 — the root is a thin catalog router with no formatter state of its own.
struct ContentView: View {
// ...
}Ownership and state
classDiagram
ContentView *-- DatesView : navigation destination
ContentView *-- MeasurementsView : destination
ContentView *-- NamesView : destination
ContentView *-- NumbersView : destination
NumbersView *-- FormatterState : State
FormatterState *-- NumberFormatter : private
DateFormatterView *-- FormatterState : State
FormatterState *-- DateFormatter : private
Ownership evidence
Shared/NumbersView.swift:88 — a private nested value owns the mutable formatter and keeps it synchronized through property observers.
private struct FormatterState {
// ...
}Each destination owns its interaction state; there is no shared app model. A nested state wrapper is the mutation authority for its formatter configuration and derived output. SwiftUI owns view lifetime and re-rendering.
Class and protocol design
| Type group | Responsibility |
|---|---|
FormattersApp / ContentView |
Composition root and catalog navigation. |
| Date, measurement, name, and number views | Demonstrate one formatter family and own controls. |
Nested FormatterState values |
Keep a mutable Foundation formatter consistent with UI selections. |
Monogram and small helpers |
Reusable presentation for formatted results. |
Platform writeToPasteboard functions |
Hide UIKit/AppKit pasteboard differences. |
There is no app-defined protocol layer. SwiftUI View composition and concrete Foundation formatter APIs are the intended teaching surface.
Access control
| Boundary | Effect and rationale |
|---|---|
private @State selections |
Only the demonstration view mutates its controls. |
private nested formatter state and formatter object |
Prevents other features from depending on one demo’s configuration mechanics. |
Implicit internal views and utilities |
Shared source is available to app targets without becoming a public library. |
| Platform utility functions share a signature | Call sites stay platform-neutral while target membership selects the implementation. |
Shared/Dates/DateFormatterView.swift:138 shows the private date state type; iOS/Utilities.swift:10 and macOS/Utilities.swift:10 show the target-specific adapter boundary.
Logic ownership and placement
| Logic | Owner |
|---|---|
| Catalog navigation | ContentView |
| Formatter configuration and derived strings | Feature-local state or view |
| Domain sample values | The feature file that demonstrates them |
| Cross-platform pasteboard write | Target-specific Utilities.swift |
| Visual monogram generation | Name component view/helper |
Design patterns
| Pattern | Evidence | Purpose |
|---|---|---|
| Catalog | Shared/ContentView.swift:12 |
Keeps unrelated formatter examples independently navigable. |
| View-local state | Shared/NumbersView.swift:11 |
Gives each example isolated lifetime and mutation. |
| State wrapper | Shared/Dates/DateFormatterView.swift:144 |
Synchronizes several selections with one formatter instance. |
| Platform adapter | iOS/Utilities.swift:10, macOS/Utilities.swift:10 |
Presents one call shape over two pasteboard APIs. |
Naming conventions
- Feature files and views are named after Foundation types:
DateFormatterView,RelativeDateTimeFormatterView, andNumbersView. - Nested
FormatterStatecommunicates local scope rather than a reusable domain model. - Platform-specific files keep the same
Utilities.swiftand function name to make target substitution invisible to callers.
Architecture takeaways
- A documentation catalog benefits from self-contained feature views, not global state.
- Wrap mutable formatter configuration close to the controls that drive it.
- Use same-signature platform adapters for tiny UIKit/AppKit differences.
- Do not infer a service or MVVM layer where the source intentionally demonstrates direct framework APIs.
Source map
| Source | Role |
|---|---|
Shared/FormattersApp.swift:10 |
Composition root |
Shared/ContentView.swift:10 |
Catalog navigation |
Shared/NumbersView.swift:10 |
Representative local formatter state |
Shared/Dates/DateFormatterView.swift:13 |
Date formatting example |
macOS/Utilities.swift:10 |
AppKit pasteboard adapter |