Defining data relationships with enumerations and model classes
At a glance
| Item | Summary |
|---|---|
| Purpose | Create relationships for static and dynamic data stored in your app. |
| App architecture | A Swift sample with the source-visible chain SwiftDataAnimalsApp → ContentView → SwiftData APIs. |
| Main patterns | Binding-based state propagation |
| Project style | 14 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: @MainActor, Task closure isolated to MainActor, Task; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: @Observable, SwiftUI state property wrapper. |
| Key frameworks/packages | SwiftData, SwiftUI, Foundation; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── SwiftDataAnimals/
├── SwiftDataAnimalsApp.swift
├── Views/
│ ├── AnimalListView.swift
│ ├── AnimalCategoryListView.swift
│ ├── AnimalDetailView.swift
│ ├── ContentView.swift
│ ├── ThreeColumnContentView.swift
│ └── AnimalEditor.swift
├── Models/
│ ├── Animal.swift
│ ├── AnimalCategory.swift
│ └── NavigationContext.swift
└── PreviewHelper/
├── ModelContainerPreview.swift
└── Preview+ModelContainer.swift
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: Swift.
- The verified tree contains 3 project/configuration file(s) and 16 source declaration(s).
Overall architecture
flowchart LR
N1["SwiftDataAnimalsApp"]
N2["ContentView"]
N3["SwiftData APIs"]
N1 --> N2
N2 --> N3
Reference code
SwiftDataAnimals/SwiftDataAnimalsApp.swift:11 — architecture anchor
@main
struct SwiftDataAnimalsApp: App {
var body: some Scene {
WindowGroup() {
ContentView()
}
.modelContainer(for: AnimalCategory.self)
#if os(macOS)
.commands {
SidebarCommands()
}
#endif
}
}Interpretation
The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into SwiftData.
Ownership and state
classDiagram
AnimalListView *-- String : animalCategoryName
AnimalList *-- String : animalCategoryName
AnimalList *-- Array : animals
AddAnimalButton o-- Bool : isActive
Ownership evidence
SwiftDataAnimals/Views/AnimalListView.swift:12 — stored dependency or nearest verified ownership anchor
struct AnimalListView: View {
let animalCategoryName: String?
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
AnimalListView |
String (animalCategoryName) |
owns value state | Initialized by the owner; the binding is immutable |
AnimalList |
String (animalCategoryName) |
owns value state | Initialized by the owner; the binding is immutable |
AnimalList |
Array (animals) |
owns value state | Owning lexical scope |
AddAnimalButton |
Bool (isActive) |
borrows mutable state | The upstream binding owner is authoritative |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Concurrency, scheduling, and thread safety
Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.
| Concern | Source mechanism | Verified placement or handoff | Evidence |
|---|---|---|---|
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | SwiftDataAnimals/PreviewHelper/Preview+ModelContainer.swift:16 |
| Main isolation | Task closure isolated to MainActor |
The cited operation explicitly enters a main-actor-isolated region. | SwiftDataAnimals/PreviewHelper/Preview+ModelContainer.swift:16 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | SwiftDataAnimals/PreviewHelper/Preview+ModelContainer.swift:16 |
@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.
Reference code
SwiftDataAnimals/PreviewHelper/Preview+ModelContainer.swift:16 — representative execution boundary
Task { @MainActor in
AnimalCategory.insertSampleData(modelContext: container.mainContext)
}State propagation, frameworks, and dependencies
Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.
| Category | Mechanism or module | Verified role | Evidence |
|---|---|---|---|
| State propagation | @Observable |
Observation macro publishes source-visible changes. | SwiftDataAnimals/Models/NavigationContext.swift:10 |
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | SwiftDataAnimals/Views/AnimalCategoryListView.swift:12 |
| Source import | SwiftData |
The cited file imports this module; runtime use and architectural role are not inferred. | SwiftDataAnimals/Models/Animal.swift:9 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | SwiftDataAnimals/Models/NavigationContext.swift:8 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | SwiftDataAnimals/Models/Animal.swift:8 |
receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.
Class and protocol design
SwiftDataAnimals/SwiftDataAnimalsApp.swift:12 — representative type boundary
@main
struct SwiftDataAnimalsApp: App {
// ...
ContentView()
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
SwiftDataAnimalsApp |
Application entry and top-level composition | App |
AnimalListView |
User-interface presentation and input forwarding | View |
AnimalCategoryListView |
User-interface presentation and input forwarding | View |
AnimalDetailView |
User-interface presentation and input forwarding | View |
AnimalDetailContentView |
User-interface presentation and input forwarding | View |
ContentView |
User-interface presentation and input forwarding | View |
ThreeColumnContentView |
User-interface presentation and input forwarding | View |
AnimalList |
Represents a feature value or composable behavior | View |
AddAnimalButton |
Represents a feature value or composable behavior | View |
ListCategories |
Represents a feature value or composable behavior | View |
No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
navigationContext (SwiftDataAnimals/Views/AnimalCategoryListView.swift:12) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
modelContext (SwiftDataAnimals/Views/AnimalCategoryListView.swift:13) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
animalCategories (SwiftDataAnimals/Views/AnimalCategoryListView.swift:14) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
isReloadPresented (SwiftDataAnimals/Views/AnimalCategoryListView.swift:15) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
Reference code
SwiftDataAnimals/Views/AnimalCategoryListView.swift:12 — representative boundary
struct AnimalCategoryListView: View {
@Environment(NavigationContext.self) private var navigationContext
// ...
}Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Application entry and top-level composition | SwiftDataAnimalsApp |
The source’s App suffix makes this role explicit. |
| User-interface presentation and input forwarding | AnimalCategoryListView, AnimalDetailContentView, AnimalDetailView, AnimalListView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Binding-based state propagation | SwiftDataAnimals/Views/AnimalListView.swift:77 |
A binding exposes controlled read/write access while the upstream owner remains authoritative. |
Naming conventions
- Types: App: SwiftDataAnimalsApp; View: AnimalCategoryListView, AnimalDetailContentView, AnimalDetailView, AnimalListView, ContentView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
removeAnimals,reloadSampleData,delete. - Files:
SwiftDataAnimals/SwiftDataAnimalsApp.swift,SwiftDataAnimals/Views/AnimalListView.swift,SwiftDataAnimals/Views/AnimalCategoryListView.swift,SwiftDataAnimals/Views/AnimalDetailView.swift,SwiftDataAnimals/Views/ContentView.swift,SwiftDataAnimals/Views/ThreeColumnContentView.swift.
Architecture takeaways
SwiftDataAnimalsAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftData, SwiftUI through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- The source does not justify labeling the design protocol-oriented.
Source map
| Source file | Relevant symbols |
|---|---|
SwiftDataAnimals/SwiftDataAnimalsApp.swift |
Cited implementation, SwiftDataAnimalsApp |
SwiftDataAnimals/Views/AnimalListView.swift |
Cited implementation, AnimalListView, AnimalList, AddAnimalButton |
SwiftDataAnimals/Views/AnimalCategoryListView.swift |
Cited implementation, SwiftUI state property wrapper, AnimalCategoryListView, ListCategories |
SwiftDataAnimals/PreviewHelper/Preview+ModelContainer.swift |
@MainActor, Task closure isolated to MainActor, Task, Feature implementation |
SwiftDataAnimals/Models/NavigationContext.swift |
@Observable, SwiftUI, NavigationContext |
SwiftDataAnimals/Models/Animal.swift |
SwiftData, Foundation, Animal, Diet |
SwiftDataAnimals/Views/AnimalDetailView.swift |
AnimalDetailView, AnimalDetailContentView |
SwiftDataAnimals/Views/ContentView.swift |
ContentView |
SwiftDataAnimals/Views/ThreeColumnContentView.swift |
ThreeColumnContentView |
SwiftDataAnimals/Models/AnimalCategory.swift |
AnimalCategory |
SwiftDataAnimals/PreviewHelper/ModelContainerPreview.swift |
ModelContainerPreview |
SwiftDataAnimals/Views/AnimalEditor.swift |
AnimalEditor |
SwiftDataAnimals/SampleData/SampleData+Animal.swift |
Feature implementation |
SwiftDataAnimals/SampleData/SampleData+AnimalCategory.swift |
Feature implementation |