Lesson 5 of 9 · 27m
Observation, Navigation, and Testable SwiftUI Architecture
Structure feature models, navigation paths, dependencies, and preview data so SwiftUI screens remain inspectable and testable as the app grows.
Architecture debates collapse quickly in iOS teams because the names are bigger than the problems. MVVM, TCA, MVC, clean architecture — all can work, and all can become ceremony. What matters is simpler: state has an owner, side effects have a boundary, navigation is data-driven, and a feature can be exercised without manual tapping.
Models should expose intent, not property soup
A view model that is only a bag of published properties leaves business rules scattered through views. Expose user intent:
@MainActor
final class DocumentListModel: ObservableObject {
@Published private(set) var documents: [Document] = []
@Published private(set) var state: LoadState<Void> = .idle
private let service: DocumentService
init(service: DocumentService) { self.service = service }
func refresh() async {
state = .loading
do {
documents = try await service.documents()
state = .loaded(())
} catch {
state = .failed(error)
}
}
}The view calls refresh(); it does not know whether that means cache first, retry, authenticated refresh, or backoff. That is the point.
Protocols are seams, not decoration
A protocol earns its place when it creates a useful substitution boundary, such as func documents() async throws -> [Document] on a DocumentService protocol. The live implementation talks to the network; tests use a scripted fake; previews use static data. Time, randomness, persistence, networking, purchases, and permissions are useful seams. Do not create one protocol per class because a blog post said so.
Make navigation data-driven
Navigation scattered across booleans cannot be deep-linked or restored. Prefer typed routes:
enum AppRoute: Hashable {
case document(Document.ID)
case settings
case invite(UUID)
}
NavigationStack(path: $path) {
DocumentListView()
.navigationDestination(for: AppRoute.self) { route in
switch route {
case .document(let id): DocumentDetailView(documentID: id)
case .settings: SettingsView()
case .invite(let id): InviteView(inviteID: id)
}
}
}Deep links become state transformations instead of heroic view hierarchy surgery.
Previews and tests are architecture feedback
If a preview cannot render without production credentials, the screen is too coupled:
struct PreviewDocumentService: DocumentService {
func documents() async throws -> [Document] { [.sampleInvoice, .sampleContract] }
}
#Preview {
DocumentListView(model: DocumentListModel(service: PreviewDocumentService()))
}Unit-test loading, retries, cancellation, validation, and navigation commands. UI tests should prove critical journeys, not replace fast logic tests.
Field notes
- Consistency inside a feature matters more than ideological purity across the app.
- Initializer injection is boring, visible, and superior to hidden singleton lookups during testing.
- If deep links and push notifications cannot reuse normal navigation code, navigation is probably event-driven rather than state-driven.
Was this lesson helpful?