Building a document-based app with SwiftUI
At a glance
| Item | Summary |
|---|---|
| Purpose | Create, save, and open documents in a multiplatform app. |
| App architecture | A Swift sample with the source-visible chain WritingApp → AccessoryView → WritingAppDocument → SwiftUI APIs. |
| Main patterns | Binding-based state propagation |
| Project style | 7 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
Project structure
Source bundle/
├── WritingApp/
│ ├── WritingApp.swift
│ ├── Views/
│ │ ├── AccessoryView.swift
│ │ ├── StoryView.swift
│ │ └── StorySheet.swift
│ ├── WritingAppDocument.swift
│ ├── Extensions/
│ │ ├── UTType+WritingApp.swift
│ │ └── Color.swift
│ ├── Info.plist
│ └── WritingApp.entitlements
├── Configuration/
│ └── SampleCode.xcconfig
└── WritingApp.xcodeproj/
├── .xcodesamplecode.plist
└── project.pbxproj
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 5 project/configuration file(s) and 6 source declaration(s).
Overall architecture
flowchart LR
N1["WritingApp"]
N2["AccessoryView"]
N3["WritingAppDocument"]
N4["SwiftUI APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
WritingApp/WritingApp.swift:11 — architecture anchor
@main
struct WritingApp: 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 SwiftUI.
Ownership and state
classDiagram
AccessoryView *-- CGSize : size
StoryView o-- WritingAppDocument : document
StoryView *-- Bool : isFocused
WritingAppDocument *-- String : story
Ownership evidence
WritingApp/Views/AccessoryView.swift:12 — stored dependency or nearest verified ownership anchor
@Environment(\.horizontalSizeClass) private var horizontal
@State private var size: CGSize = CGSize()| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
AccessoryView |
CGSize (size) |
owns wrapper-managed state | Owning lexical scope |
StoryView |
WritingAppDocument (document) |
borrows mutable state | The upstream binding owner is authoritative |
StoryView |
Bool (isFocused) |
owns value state | Owning lexical scope |
WritingAppDocument |
String (story) |
owns value state | 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
WritingApp/WritingApp.swift:12 — representative type boundary
struct WritingApp: App {
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
WritingApp |
Application entry and top-level composition | App |
AccessoryView |
User-interface presentation and input forwarding | View |
StoryView |
User-interface presentation and input forwarding | View |
WritingAppDocument |
Owns document data or lifecycle behavior | FileDocument |
StorySheet |
Represents a feature value or composable behavior | View |
DismissButton |
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 |
|---|---|---|---|
horizontal (WritingApp/Views/AccessoryView.swift:11) |
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. |
size (WritingApp/Views/AccessoryView.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. |
isShowingSheet (WritingApp/Views/StoryView.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. |
isFocused (WritingApp/Views/StoryView.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. |
Reference code
WritingApp/Views/AccessoryView.swift:11 — representative boundary
@Environment(\.horizontalSizeClass) private var horizontalSwift 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 | WritingApp |
The source’s App suffix makes this role explicit. |
| Owns document data or lifecycle behavior | WritingAppDocument |
The source’s Document suffix makes this role explicit. |
| User-interface presentation and input forwarding | AccessoryView, StoryView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Binding-based state propagation | WritingApp/Views/StorySheet.swift:12 |
A binding exposes controlled read/write access while the upstream owner remains authoritative. |
Naming conventions
- Types: App: WritingApp; Document: WritingAppDocument; View: AccessoryView, StoryView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
fileWrapper. - Files:
WritingApp/WritingApp.swift,WritingApp/Views/AccessoryView.swift,WritingApp/Views/StoryView.swift,WritingApp/WritingAppDocument.swift,WritingApp/Views/StorySheet.swift.
Architecture takeaways
WritingAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, UniformTypeIdentifiers 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 |
|---|---|
WritingApp/WritingApp.swift |
WritingApp |
WritingApp/Views/AccessoryView.swift |
AccessoryView |
WritingApp/Views/StoryView.swift |
StoryView |
WritingApp/WritingAppDocument.swift |
WritingAppDocument |
WritingApp/Extensions/UTType+WritingApp.swift |
Feature implementation |
WritingApp/Views/StorySheet.swift |
StorySheet, DismissButton |
WritingApp/Extensions/Color.swift |
Feature implementation |