Code-along: Elevating an app with Swift concurrency
At a glance
| Item | Summary |
|---|---|
| Purpose | Code along with the WWDC presenter to elevate a SwiftUI app with Swift concurrency. |
| App architecture | A Swift sample bundle with entry-bearing project variants End, Start, each leading to SwiftUI APIs. |
| Main patterns | Model-View-ViewModel, Actor-isolated state |
| Project style | 36 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
Project structure
Source bundle/
├── End/
│ └── SwiftSticker/
│ ├── SwiftStickerApp.swift
│ ├── ViewModel/
│ │ └── StickerViewModel.swift
│ ├── Model/
│ │ ├── PhotoProcessor.swift
│ │ ├── ProcessedPhoto.swift
│ │ └── ColorExtractor.swift
│ └── Views/
│ └── PhotoPickerView.swift
└── Start/
└── SwiftSticker/
├── SwiftStickerApp.swift
├── ViewModel/
│ └── StickerViewModel.swift
├── Model/
│ ├── PhotoProcessor.swift
│ ├── ProcessedPhoto.swift
│ └── ColorExtractor.swift
└── Views/
└── PhotoPickerView.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 6 project/configuration file(s) and 36 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["End"]
V2["Start"]
Boundary["SwiftUI APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Reference code
End/SwiftSticker/SwiftStickerApp.swift:12 — architecture anchor
@main
struct SwiftStickerApp: 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
StickerViewModel *-- Array : invalidPhotos
StickerViewModel o-- Selection : selection
PhotoPickerView *-- StickerViewModel : viewModel
PhotoProcessor *-- ColorExtractor : colorExtractor
Ownership evidence
End/SwiftSticker/ViewModel/StickerViewModel.swift:21 — stored dependency or nearest verified ownership anchor
@Observable
class StickerViewModel {
// ...
var invalidPhotos: [SelectedPhoto.ID] = []
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
StickerViewModel |
Array (invalidPhotos) |
owns value state | App/module collaborators |
StickerViewModel |
Selection (selection) |
stores or receives | App/module collaborators |
PhotoPickerView |
StickerViewModel (viewModel) |
owns wrapper-managed state | App/module collaborators |
PhotoProcessor |
ColorExtractor (colorExtractor) |
creates and retains | Initialized by the owner; the binding is immutable |
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
End/SwiftSticker/SwiftStickerApp.swift:13 — representative type boundary
struct SwiftStickerApp: App {
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
SwiftStickerApp |
Application entry and top-level composition | App |
SwiftStickerApp |
Application entry and top-level composition | App |
StickerViewModel |
UI-facing state and feature coordination | Concrete collaborators/imported frameworks |
StickerViewModel |
UI-facing state and feature coordination | Concrete collaborators/imported frameworks |
PhotoProcessor |
Owns a feature processing stage | Concrete collaborators/imported frameworks |
PhotoPickerView |
User-interface presentation and input forwarding | View |
PhotoProcessor |
Owns a feature processing stage | Concrete collaborators/imported frameworks |
PhotoPickerView |
User-interface presentation and input forwarding | View |
ProcessedPhoto |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
PhotoColorScheme |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
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 |
|---|---|---|---|
k (End/SwiftSticker/Model/ColorExtractor.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. |
sourceImage (End/SwiftSticker/Model/ColorExtractor.swift:22) |
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. |
rgbImageFormat (End/SwiftSticker/Model/ColorExtractor.swift:39) |
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. |
distances (End/SwiftSticker/Model/ColorExtractor.swift:50) |
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
End/SwiftSticker/Model/ColorExtractor.swift:15 — representative boundary
private let k = 4
static let dimension = 256
static let channelCount = 3
static let tolerance = 10Swift 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 | SwiftStickerApp |
The source’s App suffix makes this role explicit. |
| Owns a feature processing stage | PhotoProcessor |
The source’s Processor suffix makes this role explicit. |
| User-interface presentation and input forwarding | PhotoPickerView |
The source’s View suffix makes this role explicit. |
| UI-facing state and feature coordination | StickerViewModel |
The source’s ViewModel suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Model-View-ViewModel | End/SwiftSticker/ViewModel/StickerViewModel.swift:13 |
Role-named view models keep UI-facing state or coordination outside view declarations. |
| Actor-isolated state | End/SwiftSticker/ViewModel/StickerViewModel+Utilities.swift:17 |
Actor annotations make the concurrency ownership boundary explicit. |
Naming conventions
- Types: App: SwiftStickerApp; Processor: PhotoProcessor; View: PhotoPickerView; ViewModel: StickerViewModel.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
loadPhoto,processAllPhotos,process,extractColors,extractSticker. - Files:
End/SwiftSticker/SwiftStickerApp.swift,Start/SwiftSticker/SwiftStickerApp.swift,End/SwiftSticker/ViewModel/StickerViewModel.swift,Start/SwiftSticker/ViewModel/StickerViewModel.swift,End/SwiftSticker/Model/PhotoProcessor.swift,End/SwiftSticker/Views/PhotoPickerView.swift.
Architecture takeaways
SwiftStickerAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, PhotosUI, Accelerate, CoreImage 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 |
|---|---|
End/SwiftSticker/SwiftStickerApp.swift |
SwiftStickerApp |
Start/SwiftSticker/SwiftStickerApp.swift |
SwiftStickerApp |
End/SwiftSticker/ViewModel/StickerViewModel.swift |
StickerViewModel |
Start/SwiftSticker/ViewModel/StickerViewModel.swift |
StickerViewModel |
End/SwiftSticker/Model/PhotoProcessor.swift |
PhotoProcessor |
End/SwiftSticker/Views/PhotoPickerView.swift |
PhotoPickerView |
Start/SwiftSticker/Model/PhotoProcessor.swift |
PhotoProcessor |
Start/SwiftSticker/Views/PhotoPickerView.swift |
PhotoPickerView |
End/SwiftSticker/Model/ProcessedPhoto.swift |
ProcessedPhoto, PhotoColorScheme, ProcessedPhotoResult |
Start/SwiftSticker/Model/ProcessedPhoto.swift |
ProcessedPhoto, PhotoColorScheme, ProcessedPhotoResult |