Sample CodemacOSReviewed 2026-07-21View on Apple Developer

Building a great Mac app with SwiftUI

At a glance

Item Summary
Purpose Create engaging SwiftUI Mac apps by incorporating side bars, tables, toolbars, and several other popular user interface elements.
App architecture A Swift sample bundle with entry-bearing project variants Session1, Session2, each leading to SwiftUI APIs.
Main patterns Central store, SwiftUI environment injection, Binding-based state propagation, Publisher-backed observable state
Project style 72 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
├── Session1/
│   ├── Part1-End/
│   │   └── GardenApp/
│   │       ├── GardenApp.swift
│   │       ├── Navigation/
│   │       │   ├── ContentView.swift
│   │       │   └── GardenDetail.swift
│   │       └── Model/
│   │           └── Store.swift
│   └── Part1-Start/
│       └── GardenApp/
│           ├── GardenApp.swift
│           ├── Navigation/
│           │   ├── ContentView.swift
│           │   └── GardenDetail.swift
│           └── Model/
│               └── Store.swift
└── Session2/
    ├── Part2-End/
    │   └── GardenApp/
    │       ├── GardenApp.swift
    │       └── Views/
    │           └── SettingsView.swift
    └── Part2-Start/
        └── GardenApp/
            ├── GardenApp.swift
            └── Views/
                └── SettingsView.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 15 project/configuration file(s) and 95 source declaration(s).

Overall architecture

Reference code

Session1/Part1-End/GardenApp/GardenApp.swift:10 — architecture anchor

@main
struct GardenApp: 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

Ownership evidence

Session1/Part1-End/GardenApp/GardenApp.swift:13 — stored dependency or nearest verified ownership anchor

@main
struct GardenApp: App {
    // ...
    @StateObject private var store = Store()
    // ...
}
Owner Object or state Relationship Mutation authority
GardenApp Store (store) owns wrapper-managed state Owning lexical scope
ContentView Store (store) receives environment-provided state The environment provider is authoritative
ContentView ID (selectedGardenID) stores or receives Owning lexical scope
Sidebar Store (store) receives environment-provided state The environment provider 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

Session1/Part1-End/GardenApp/GardenApp.swift:11 — representative type boundary

struct GardenApp: App {
    // ...
}
Type Responsibility Depends on or conforms to
GardenApp Application entry and top-level composition App
GardenApp Application entry and top-level composition App
GardenApp Application entry and top-level composition App
GardenApp Application entry and top-level composition App
ContentView User-interface presentation and input forwarding View
SettingsView User-interface presentation and input forwarding View
SettingsView User-interface presentation and input forwarding View
ContentView User-interface presentation and input forwarding View
Store Centralized state or persistence access ObservableObject
Store Centralized state or persistence access ObservableObject

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
store (Session1/Part1-End/GardenApp/GardenApp.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.
garden (Session1/Part1-End/GardenApp/Main Menu/PlantCommands.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.
selection (Session1/Part1-End/GardenApp/Main Menu/PlantCommands.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.
applicationSupportDirectory (Session1/Part1-End/GardenApp/Model/Store.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

Session1/Part1-End/GardenApp/GardenApp.swift:13 — representative boundary

@main
struct GardenApp: App {
    // ...
    @StateObject private var store = Store()
    // ...
}

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 GardenApp The source’s App suffix makes this role explicit.
Centralized state or persistence access Store The source’s Store suffix makes this role explicit.
User-interface presentation and input forwarding ContentView, SettingsView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Central store Session1/Part1-End/GardenApp/Model/Store.swift:10 A store-named type centralizes feature state or persistence.
SwiftUI environment injection Session1/Part1-End/GardenApp/Navigation/ContentView.swift:11 The environment supplies state or a capability without threading it through every initializer.
Binding-based state propagation Session1/Part1-End/GardenApp/Navigation/ContentView.swift:34 A binding exposes controlled read/write access while the upstream owner remains authoritative.
Publisher-backed observable state Session1/Part1-End/GardenApp/Model/Store.swift:11 Published properties notify observers while mutation remains with the state object.

Naming conventions

  • Types: App: GardenApp; Store: Store; View: ContentView, SettingsView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: no representative method name extracted from the architectural files.
  • Files: Session1/Part1-End/GardenApp/GardenApp.swift, Session1/Part1-Start/GardenApp/GardenApp.swift, Session2/Part2-End/GardenApp/GardenApp.swift, Session2/Part2-Start/GardenApp/GardenApp.swift, Session1/Part1-End/GardenApp/Navigation/ContentView.swift, Session2/Part2-End/GardenApp/Views/SettingsView.swift.

Architecture takeaways

  • GardenApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, UniformTypeIdentifiers, AppKit 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
Session1/Part1-End/GardenApp/GardenApp.swift GardenApp
Session1/Part1-Start/GardenApp/GardenApp.swift GardenApp
Session2/Part2-End/GardenApp/GardenApp.swift GardenApp
Session2/Part2-Start/GardenApp/GardenApp.swift GardenApp
Session1/Part1-End/GardenApp/Navigation/ContentView.swift ContentView, ContentViewPreviews, Sidebar
Session2/Part2-End/GardenApp/Views/SettingsView.swift SettingsView, GeneralSettings, ViewingSettings
Session2/Part2-Start/GardenApp/Views/SettingsView.swift SettingsView, GeneralSettings, ViewingSettings
Session1/Part1-Start/GardenApp/Navigation/ContentView.swift ContentView, ContentViewPreviews
Session1/Part1-End/GardenApp/Model/Store.swift Store
Session1/Part1-End/GardenApp/Navigation/GardenDetail.swift GardenDetail, ViewMode, GardenDetailPreviews, BoolComparator