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

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 WritingAppAccessoryViewWritingAppDocumentSwiftUI APIs.
Main patterns Binding-based state propagation
Project style 7 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model No structured execution marker indexed; callback threading requires source review.
State/event model Source-visible mechanisms: SwiftUI state property wrapper.
Key frameworks/packages SwiftUI, UniformTypeIdentifiers; these are source dependencies, not architecture labels.

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

Reference code

WritingApp/WritingApp.swift:11 — architecture anchor

@main
struct WritingApp: App {
    var body: some Scene {
        #if os(iOS)
        DocumentGroupLaunchScene("Writing App") {
            NewDocumentButton("Start Writing")
        } background: {
            Image(.pinkJungle)
                .resizable()
                .scaledToFill()
                .ignoresSafeArea()
        } overlayAccessoryView: { _ in
            AccessoryView()
        }
        #endif
        DocumentGroup(newDocument: WritingAppDocument()) { file in
            StoryView(document: file.$document)
        }
    }
}

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

Ownership evidence

WritingApp/Views/AccessoryView.swift:12 — stored dependency or nearest verified ownership anchor

struct AccessoryView: View {
    // ...
    @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.

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.

No source-visible execution, scheduling, or synchronization boundary was found in the indexed source.

@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.

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 SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. WritingApp/Views/AccessoryView.swift:11
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. WritingApp/Extensions/Color.swift:8
Source import UniformTypeIdentifiers The cited file imports this module; runtime use and architectural role are not inferred. WritingApp/Extensions/UTType+WritingApp.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

WritingApp/WritingApp.swift:12 — representative type boundary

@main
struct WritingApp: App {
    // ...
            NewDocumentButton("Start Writing")
    // ...
}
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

struct AccessoryView: View {
    @Environment(\.horizontalSizeClass) private var horizontal
    // ...
}

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 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

  • WritingApp is 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 Cited implementation, WritingApp
WritingApp/Views/AccessoryView.swift Cited implementation, SwiftUI state property wrapper, AccessoryView
WritingApp/Views/StoryView.swift Cited implementation, StoryView
WritingApp/Views/StorySheet.swift Cited implementation, StorySheet, DismissButton
WritingApp/Extensions/Color.swift SwiftUI, Feature implementation
WritingApp/Extensions/UTType+WritingApp.swift UniformTypeIdentifiers, Feature implementation
WritingApp/WritingAppDocument.swift WritingAppDocument