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. |
| Execution model | Source-visible boundaries: DispatchQueue.main.async; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper, ObservableObject, @Published. |
| Key frameworks/packages | SwiftUI, Foundation, UniformTypeIdentifiers, AppKit; these are source dependencies, not architecture labels. |
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
flowchart LR
Bundle["Sample bundle"]
V1["Session1"]
V2["Session2"]
Boundary["SwiftUI APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Reference code
Session1/Part1-End/GardenApp/GardenApp.swift:10 — architecture anchor
@main
struct GardenApp: App {
@StateObject private var store = Store()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(store)
}
.commands {
SidebarCommands()
PlantCommands()
}
}
}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
GardenApp *-- Store : store
ContentView o-- Store : store
ContentView o-- ID : selectedGardenID
Sidebar o-- Store : store
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.
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.
| Concern | Source mechanism | Verified placement or handoff | Evidence |
|---|---|---|---|
| Queue scheduling | DispatchQueue.main.async |
The source addresses the main dispatch queue. | Session2/Part2-End/GardenApp/Model/Plant+ImportImage.swift:28 |
@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.
Reference code
Session2/Part2-End/GardenApp/Model/Plant+ImportImage.swift:28 — representative execution boundary
DispatchQueue.main.async {
completion(nil)
}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. | Session1/Part1-End/GardenApp/GardenApp.swift:13 |
| State propagation | ObservableObject |
ObservableObject supplies an observation contract. | Session1/Part1-End/GardenApp/Model/Store.swift:10 |
| State propagation | @Published |
A published property can emit owner-controlled changes. | Session1/Part1-End/GardenApp/Model/Store.swift:11 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | Session1/Part1-End/GardenApp/GardenApp.swift:8 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | Session1/Part1-End/GardenApp/Model/Garden.swift:8 |
| Source import | UniformTypeIdentifiers |
The cited file imports this module; runtime use and architectural role are not inferred. | Session2/Part2-End/GardenApp/Model/Plant+ImportImage.swift:10 |
| Source import | AppKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Session2/Part2-End/GardenApp/Model/Plant+ImportImage.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
Session1/Part1-End/GardenApp/GardenApp.swift:11 — representative type boundary
@main
struct GardenApp: App {
// ...
@StateObject private var store = Store()
// ...
}| 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
GardenAppis 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 |
Cited implementation, GardenApp, SwiftUI state property wrapper, SwiftUI |
Session1/Part1-End/GardenApp/Main Menu/PlantCommands.swift |
Cited implementation, PlantCommands |
Session1/Part1-End/GardenApp/Model/Store.swift |
Cited implementation, Store, ObservableObject, @Published |
Session1/Part1-End/GardenApp/Navigation/ContentView.swift |
Cited implementation, ContentView, ContentViewPreviews, Sidebar |
Session2/Part2-End/GardenApp/Model/Plant+ImportImage.swift |
DispatchQueue.main.async, UniformTypeIdentifiers, AppKit, Feature implementation |
Session1/Part1-End/GardenApp/Model/Garden.swift |
Foundation, Garden |
Session1/Part1-Start/GardenApp/GardenApp.swift |
GardenApp |
Session2/Part2-End/GardenApp/GardenApp.swift |
GardenApp |
Session2/Part2-Start/GardenApp/GardenApp.swift |
GardenApp |
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/Navigation/GardenDetail.swift |
GardenDetail, ViewMode, GardenDetailPreviews, BoolComparator |
Session1/Part1-Start/GardenApp/Model/Store.swift |
Store |
Session1/Part1-Start/GardenApp/Navigation/GardenDetail.swift |
GardenDetail, ViewMode, GardenDetailPreviews, BoolComparator |
Session2/Part2-End/GardenApp/Model/Store.swift |
Store |