Building rich SwiftUI text experiences
At a glance
| Item | Summary |
|---|---|
| Purpose | Build an editor for formatted text using SwiftUI text editor views and attributed strings. |
| App architecture | A Swift sample with the source-visible chain SampleRecipeEditorApp → RootView → AttributedStringModel → SwiftUI APIs. |
| Main patterns | Binding-based state propagation |
| Project style | 17 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: await suspension point, Sendable or @Sendable, Task, @MainActor; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: NotificationCenter, SwiftUI state property wrapper, @Observable. |
| Key frameworks/packages | SwiftUI, SwiftData, Foundation, PhotosUI, UniformTypeIdentifiers; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── SampleRecipeEditor/
└── SampleRecipeEditor/
├── SampleRecipeEditorApp.swift
├── AttributeScopes.swift
├── AttributedStringModel.swift
├── BackgroundView.swift
├── InspectorView.swift
├── NavigationView.swift
├── RecipeFormattingDefinition.swift
├── RecipeView.swift
├── IngredientList.swift
├── RecipeList.swift
├── DispatchingChanges.swift
└── EditableRecipeText.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 4 project/configuration file(s) and 32 source declaration(s).
Overall architecture
flowchart LR
N1["SampleRecipeEditorApp"]
N2["RootView"]
N3["AttributedStringModel"]
N4["SwiftUI APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
SampleRecipeEditor/SampleRecipeEditor/SampleRecipeEditorApp.swift:12 — architecture anchor
@main
struct SampleRecipeEditorApp: App {
var body: some Scene {
WindowGroup {
RootView()
}
.modelContainer(for: Recipe.self)
}
}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
IngredientNameAttributes o-- AdaptiveImageGlyphAttribute : adaptiveImageGlyph
RecipeModelAttributes o-- CustomAttributes : custom
RecipeModelAttributes o-- FontAttribute : font
RecipeModelAttributes o-- IngredientNameAttributes : ingredientName
Ownership evidence
SampleRecipeEditor/SampleRecipeEditor/AttributeScopes.swift:17 — stored dependency or nearest verified ownership anchor
struct IngredientNameAttributes: AttributeScope {
let adaptiveImageGlyph: AttributeScopes.SwiftUIAttributes.AdaptiveImageGlyphAttribute
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
IngredientNameAttributes |
AdaptiveImageGlyphAttribute (adaptiveImageGlyph) |
stores or receives | Initialized by the owner; the binding is immutable |
RecipeModelAttributes |
CustomAttributes (custom) |
stores or receives | Initialized by the owner; the binding is immutable |
RecipeModelAttributes |
FontAttribute (font) |
stores or receives | Initialized by the owner; the binding is immutable |
RecipeModelAttributes |
IngredientNameAttributes (ingredientName) |
stores or receives | 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.
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 |
|---|---|---|---|
| Suspension boundary | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | SampleRecipeEditor/SampleRecipeEditor/BackgroundView.swift:67 |
| Transfer contract | Sendable or @Sendable |
The source declares a sendability boundary; this alone does not synchronize mutable state. | SampleRecipeEditor/SampleRecipeEditor/DispatchingChanges.swift:11 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | SampleRecipeEditor/SampleRecipeEditor/DispatchingChanges.swift:44 |
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | SampleRecipeEditor/SampleRecipeEditor/EditableRecipeText.swift:10 |
@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
SampleRecipeEditor/SampleRecipeEditor/BackgroundView.swift:67 — representative execution boundary
struct ImageWithPlaceholder<Placeholder: View>: View {
// ...
self.loadedImage = try? await Image(importing: imageData, contentType: .image)
// ...
}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 | NotificationCenter |
NotificationCenter distributes named process-local events. | SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift:42 |
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | SampleRecipeEditor/SampleRecipeEditor/BackgroundView.swift:44 |
| State propagation | @Observable |
Observation macro publishes source-visible changes. | SampleRecipeEditor/SampleRecipeEditor/EditableRecipeText.swift:11 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | SampleRecipeEditor/SampleRecipeEditor/AttributeScopes.swift:10 |
| Source import | SwiftData |
The cited file imports this module; runtime use and architectural role are not inferred. | SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift:9 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | SampleRecipeEditor/SampleRecipeEditor/AttributeScopes.swift:9 |
| Source import | PhotosUI |
The cited file imports this module; runtime use and architectural role are not inferred. | SampleRecipeEditor/SampleRecipeEditor/RecipeSettings.swift:10 |
| Source import | UniformTypeIdentifiers |
The cited file imports this module; runtime use and architectural role are not inferred. | SampleRecipeEditor/SampleRecipeEditor/BackgroundView.swift:9 |
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
SampleRecipeEditor/SampleRecipeEditor/SampleRecipeEditorApp.swift:13 — representative type boundary
@main
struct SampleRecipeEditorApp: App {
// ...
RootView()
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
SampleRecipeEditorApp |
Application entry and top-level composition | App |
AttributedStringModel |
Feature data or observable state | Identifiable |
BackgroundView |
User-interface presentation and input forwarding | View |
InspectorView |
User-interface presentation and input forwarding | View |
RootView |
User-interface presentation and input forwarding | View |
RecipeView |
User-interface presentation and input forwarding | View |
IngredientNameAttributes |
Represents a feature value or composable behavior | AttributeScope |
RecipeModelAttributes |
Represents a feature value or composable behavior | AttributeScope |
RecipeEditorAttributes |
Represents a feature value or composable behavior | AttributeScope |
CustomAttributes |
Represents a feature value or composable behavior | AttributeScope |
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 |
|---|---|---|---|
valueChanged (SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift:24) |
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. |
data (SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift:27) |
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. |
scope (SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift:29) |
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. |
initializeValue (SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift:47) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: hide an implementation step that is not part of the collaboration surface. |
Reference code
SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift:24 — representative boundary
@Model
final class AttributedStringModel: Identifiable {
// ...
private var valueChanged: Bool = false
// ...
}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 | SampleRecipeEditorApp |
The source’s App suffix makes this role explicit. |
| Feature data or observable state | AttributedStringModel |
The source’s Model suffix makes this role explicit. |
| User-interface presentation and input forwarding | BackgroundView, InspectorView, RecipeView, RootView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Binding-based state propagation | SampleRecipeEditor/SampleRecipeEditor/IngredientList.swift:18 |
A binding exposes controlled read/write access while the upstream owner remains authoritative. |
Naming conventions
- Types: App: SampleRecipeEditorApp; Model: AttributedStringModel; View: BackgroundView, InspectorView, RecipeView, RootView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
initializeValue,willSave,hash,addIngredient,constrain. - Files:
SampleRecipeEditor/SampleRecipeEditor/SampleRecipeEditorApp.swift,SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift,SampleRecipeEditor/SampleRecipeEditor/BackgroundView.swift,SampleRecipeEditor/SampleRecipeEditor/InspectorView.swift,SampleRecipeEditor/SampleRecipeEditor/RecipeFormattingDefinition.swift,SampleRecipeEditor/SampleRecipeEditor/RecipeView.swift.
Architecture takeaways
SampleRecipeEditorAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, SwiftData, PhotosUI, 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 |
|---|---|
SampleRecipeEditor/SampleRecipeEditor/SampleRecipeEditorApp.swift |
Cited implementation, SampleRecipeEditorApp |
SampleRecipeEditor/SampleRecipeEditor/AttributeScopes.swift |
Cited implementation, SwiftUI, Foundation, IngredientNameAttributes, RecipeModelAttributes, RecipeEditorAttributes, CustomAttributes, ParagraphFormat, ParagraphFormattingAttribute, IngredientAttribute |
SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift |
Cited implementation, NotificationCenter, SwiftData, AttributedStringModel, Scope |
SampleRecipeEditor/SampleRecipeEditor/IngredientList.swift |
Cited implementation, IngredientsList, NewIngredientRow, IngredientToggleStyle |
SampleRecipeEditor/SampleRecipeEditor/BackgroundView.swift |
await suspension point, SwiftUI state property wrapper, UniformTypeIdentifiers, BackgroundView, ImageWithPlaceholder |
SampleRecipeEditor/SampleRecipeEditor/DispatchingChanges.swift |
Sendable or @Sendable, Task, DispatchingChanges |
SampleRecipeEditor/SampleRecipeEditor/EditableRecipeText.swift |
@MainActor, @Observable, EditableRecipeText |
SampleRecipeEditor/SampleRecipeEditor/RecipeSettings.swift |
PhotosUI, RecipeSettings |
SampleRecipeEditor/SampleRecipeEditor/InspectorView.swift |
IngredientSuggestion, InspectorView |
SampleRecipeEditor/SampleRecipeEditor/NavigationView.swift |
RootView |
SampleRecipeEditor/SampleRecipeEditor/RecipeFormattingDefinition.swift |
RecipeFormattingDefinition, NormalizeFonts, IngredientsAreGreen, BoldUsesAccentColor |
SampleRecipeEditor/SampleRecipeEditor/RecipeView.swift |
RecipeView |
SampleRecipeEditor/SampleRecipeEditor/RecipeList.swift |
RecipeList, NameField |
SampleRecipeEditor/SampleRecipeEditor/Ingredient.swift |
Ingredient |
SampleRecipeEditor/SampleRecipeEditor/Recipe.swift |
Recipe |
SampleRecipeEditor/SampleRecipeEditor/RecipeEditor.swift |
RecipeEditor |