Backyard Birds: Building an app with SwiftData and widgets
At a glance
| Item | Summary |
|---|---|
| Purpose | Create an app with persistent data, interactive widgets, and an all new in-app purchase experience. |
| App architecture | A Swift sample bundle with entry-bearing project variants Multiplatform, Watch, Widgets, each leading to SwiftUI APIs. |
| Main patterns | Actor-isolated state |
| Project style | 114 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
Project structure
Source bundle/
├── Multiplatform/
│ ├── BackyardBirdsApp.swift
│ └── Backyards/
│ └── BackyardDetailView.swift
├── Watch/
│ └── BackyardBirdsWatchApp.swift
├── Widgets/
│ ├── WidgetsBundle.swift
│ └── Backyard/
│ ├── BackyardSnapshotTimelineProvider.swift
│ └── BackyardWidgetView.swift
├── BackyardBirdsData/
│ └── General/
│ └── SeededRandomGenerator.swift
└── BackyardBirdsUI/
├── Backyard/
│ ├── BackyardSkyView.swift
│ ├── BackyardViewport.swift
│ └── BackyardViewportLayout.swift
├── PlantView.swift
└── Visitors/
└── RecentBackyardVisitorsView.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 8 project/configuration file(s) and 135 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["Multiplatform"]
V2["Watch"]
V3["Widgets"]
Boundary["SwiftUI APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Bundle --> V3
V3 --> Boundary
Reference code
Multiplatform/BackyardBirdsApp.swift:12 — architecture anchor
@main
struct BackyardBirdsApp: App {
// ...
}Interpretation
The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.
Ownership and state
classDiagram
BackyardSnapshot o-- Backyard : backyard
BackyardSnapshot o-- Bird : visitingBird
BackyardSnapshot o-- TimeInterval : visitingBirdDuration
BackyardSnapshot o-- TimeInterval : timeInterval
Ownership evidence
BackyardBirdsData/Backyards/BackyardSnapshot.swift:11 — stored dependency or nearest verified ownership anchor
public var backyard: Backyard
public var visitingBird: Bird?
public var visitingBirdDuration: TimeInterval?
public var timeInterval: TimeInterval
public var date: Date
public var notableEvents: Set<NotableEvent> = []| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
BackyardSnapshot |
Backyard (backyard) |
stores or receives | App/module collaborators |
BackyardSnapshot |
Bird (visitingBird) |
stores or receives | App/module collaborators |
BackyardSnapshot |
TimeInterval (visitingBirdDuration) |
stores or receives | App/module collaborators |
BackyardSnapshot |
TimeInterval (timeInterval) |
stores or receives | App/module collaborators |
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
Multiplatform/BackyardBirdsApp.swift:13 — representative type boundary
struct BackyardBirdsApp: App {
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
BackyardBirdsApp |
Application entry and top-level composition | App |
BackyardBirdsWatchApp |
Application entry and top-level composition | App |
BackyardSnapshotTimelineProvider |
Supplies a capability or framework resource | AppIntentTimelineProvider |
BackyardWidgetView |
User-interface presentation and input forwarding | View |
BackyardSnapshotWidgetView |
User-interface presentation and input forwarding | View |
SeededRandomGenerator |
Generates feature data or resources | RandomNumberGenerator |
BackyardSkyView |
User-interface presentation and input forwarding | View |
PlantView |
User-interface presentation and input forwarding | View |
RecentBackyardVisitorsView |
User-interface presentation and input forwarding | View |
BackyardDetailView |
User-interface presentation and input forwarding | 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 |
|---|---|---|---|
logger (BackyardBirdsData/Account/Account+DataGeneration.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. |
logger (BackyardBirdsData/Account/Account.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. |
id (BackyardBirdsData/Account/Account.swift:17) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
bird (BackyardBirdsData/Account/Account.swift:18) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
Reference code
BackyardBirdsData/Account/Account+DataGeneration.swift:12 — representative boundary
private let logger = Logger(subsystem: "BackyardBirdsData", category: "Account Generation")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 | BackyardBirdsApp, BackyardBirdsWatchApp |
The source’s App suffix makes this role explicit. |
| Generates feature data or resources | SeededRandomGenerator |
The source’s Generator suffix makes this role explicit. |
| Supplies a capability or framework resource | BackyardSnapshotTimelineProvider |
The source’s Provider suffix makes this role explicit. |
| User-interface presentation and input forwarding | AppTabView, BackyardDetailView, BackyardSkyView, BackyardSnapshotWidgetView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Actor-isolated state | Multiplatform/Shop/BirdBrain.swift:16 |
Actor annotations make the concurrency ownership boundary explicit. |
Naming conventions
- Types: App: BackyardBirdsApp, BackyardBirdsWatchApp; Generator: SeededRandomGenerator; Provider: BackyardSnapshotTimelineProvider; View: AppTabView, BackyardDetailView, BackyardSkyView, BackyardSnapshotWidgetView, BackyardTabView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
backyards,placeholder,snapshot,timeline,recommendations,next,body,backyardViewportContent. - Files:
Multiplatform/BackyardBirdsApp.swift,Watch/BackyardBirdsWatchApp.swift,Widgets/WidgetsBundle.swift,Widgets/Backyard/BackyardSnapshotTimelineProvider.swift,Widgets/Backyard/BackyardWidgetView.swift,BackyardBirdsData/General/SeededRandomGenerator.swift.
Architecture takeaways
BackyardBirdsAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, SwiftData, BackyardBirdsData, BackyardBirdsUI 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 |
|---|---|
Multiplatform/BackyardBirdsApp.swift |
BackyardBirdsApp |
Watch/BackyardBirdsWatchApp.swift |
BackyardBirdsWatchApp |
Widgets/WidgetsBundle.swift |
WidgetsBundle |
Widgets/Backyard/BackyardSnapshotTimelineProvider.swift |
BackyardWidgetEntry, BackyardSnapshotTimelineProvider |
Widgets/Backyard/BackyardWidgetView.swift |
BackyardWidgetView, BackyardSnapshotWidgetView |
BackyardBirdsData/General/SeededRandomGenerator.swift |
SeededRandomGenerator |
BackyardBirdsUI/Backyard/BackyardSkyView.swift |
BackyardSkyView |
BackyardBirdsUI/Backyard/BackyardViewport.swift |
BirdAnimation, BackyardViewport, PlantPlacement, BackyardViewportContentModifier |
BackyardBirdsUI/Backyard/BackyardViewportLayout.swift |
BackyardViewportContent, BackyardViewportContentKey, BackyardViewportLayout, LayoutResult |
BackyardBirdsUI/PlantView.swift |
PlantView |