Sample CodeiOS, iPadOS, Mac Catalyst, macOS, tvOSReviewed 2026-07-21View on Apple Developer

Adding and editing persistent data in your app

At a glance

Item Summary
Purpose Create a data entry form for collecting and changing data managed by SwiftData.
App architecture A Swift sample with the source-visible chain SwiftDataAnimalsAppContentViewSwiftData APIs.
Main patterns Binding-based state propagation, Actor-isolated state
Project style 14 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.

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

Reference code

SwiftDataAnimals/SwiftDataAnimalsApp.swift:11 — architecture anchor

@main
struct SwiftDataAnimalsApp: App {
    // ...
}

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

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.

Class and protocol design

SwiftDataAnimals/SwiftDataAnimalsApp.swift:12 — representative type boundary

struct SwiftDataAnimalsApp: App {
    // ...
}
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

    @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.
Actor-isolated state SwiftDataAnimals/PreviewHelper/ModelContainerPreview.swift:29 Actor annotations make the concurrency ownership boundary explicit.

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

  • SwiftDataAnimalsApp is 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 SwiftDataAnimalsApp
SwiftDataAnimals/Views/AnimalListView.swift AnimalListView, AnimalList, AddAnimalButton
SwiftDataAnimals/Views/AnimalCategoryListView.swift AnimalCategoryListView, ListCategories
SwiftDataAnimals/Views/AnimalDetailView.swift AnimalDetailView, AnimalDetailContentView
SwiftDataAnimals/Views/ContentView.swift ContentView
SwiftDataAnimals/Views/ThreeColumnContentView.swift ThreeColumnContentView
SwiftDataAnimals/Models/Animal.swift Animal, Diet
SwiftDataAnimals/Models/AnimalCategory.swift AnimalCategory
SwiftDataAnimals/Models/NavigationContext.swift NavigationContext
SwiftDataAnimals/PreviewHelper/ModelContainerPreview.swift ModelContainerPreview