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

Recognizing tables within a document

At a glance

Item Summary
Purpose Scan a document that contains a table and extract its content in a formatted way.
App architecture A Swift sample with the source-visible chain DocumentReaderAppContentViewVisionModelCaptureServiceVision APIs.
Main patterns Protocol-oriented abstraction, Delegate or data-source callbacks, Service object, Binding-based state propagation, Publisher-backed observable state, Actor isolation
Project style 12 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: async declaration or closure, @MainActor, Task closure isolated to MainActor, Task, Sendable or @Sendable; none alone proves a background thread.
State/event model Source-visible mechanisms: @Observable, @Published, SwiftUI state property wrapper.
Key frameworks/packages SwiftUI, Vision, AVFoundation, AVKit, Combine; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── DocumentReaderApp.swift
├── DataModel.swift
├── Camera/
│   ├── CameraPreview.swift
│   ├── CaptureService.swift
│   ├── PhotoCapture.swift
│   └── Camera.swift
├── Views/
│   ├── CameraView.swift
│   ├── ImageView.swift
│   ├── ContactView.swift
│   └── ContentView.swift
├── VisionModel.swift
└── BoundingBox.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 3 project/configuration file(s) and 26 source declaration(s).

Overall architecture

Reference code

DocumentReaderApp.swift:10 — architecture anchor

@main
struct DocumentReaderApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

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

Ownership and state

Ownership evidence

DataModel.swift:13 — stored dependency or nearest verified ownership anchor

struct Contact {
    let name: String
    let email: String
    let phoneNumber: String?
}
Owner Object or state Relationship Mutation authority
Contact String (name) owns value state Initialized by the owner; the binding is immutable
Contact String (email) owns value state Initialized by the owner; the binding is immutable
Contact String (phoneNumber) owns value state Initialized by the owner; the binding is immutable
TableCell Content (content) 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 async declaration or closure The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. Camera/Camera.swift:39
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. Camera/CameraPreview.swift:65
Main isolation Task closure isolated to MainActor The cited operation explicitly enters a main-actor-isolated region. Camera/CameraPreview.swift:65
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. Camera/CameraPreview.swift:65
Transfer contract Sendable or @Sendable The source declares a sendability boundary; this alone does not synchronize mutable state. Camera/CameraPreview.swift:78

@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

Camera/Camera.swift:39 — representative execution boundary

    func start() async {
        // ...
            status = .unauthorized
        // ...
    }

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 @Observable Observation macro publishes source-visible changes. Camera/Camera.swift:11
State propagation @Published A published property can emit owner-controlled changes. Camera/PhotoCapture.swift:27
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. Views/CameraView.swift:12
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. BoundingBox.swift:8
Source import Vision The cited file imports this module; runtime use and architectural role are not inferred. BoundingBox.swift:9
Source import AVFoundation The cited file imports this module; runtime use and architectural role are not inferred. Camera/CameraPreview.swift:9
Source import AVKit The cited file imports this module; runtime use and architectural role are not inferred. Views/CameraView.swift:9
Source import Combine The cited file imports this module; runtime use and architectural role are not inferred. Camera/CaptureService.swift:10

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

Camera/CameraPreview.swift:78 — representative type boundary

protocol PreviewSource: Sendable {
    // Connects a preview destination to this source.
    func connect(to target: PreviewTarget)
}
Type Responsibility Depends on or conforms to
DocumentReaderApp Application entry and top-level composition App
PreviewView User-interface presentation and input forwarding UIView, PreviewTarget
CaptureService Framework-facing operations Concrete collaborators/imported frameworks
CameraView User-interface presentation and input forwarding View
ImageView User-interface presentation and input forwarding View
VisionModel Feature data or observable state Concrete collaborators/imported frameworks
PhotoCaptureDelegate Receives callback-driven events NSObject, AVCapturePhotoCaptureDelegate
ContactView User-interface presentation and input forwarding View
ContentView User-interface presentation and input forwarding View
PreviewSource Defines a capability or collaboration contract Sendable

The source explicitly defines local protocol relationships: PreviewViewPreviewTarget, DefaultPreviewSourcePreviewSource.

Access control

Symbol Access Verified effect Likely rationale
status (Camera/Camera.swift:29) private(set) Read access follows the declaration; writes remain in the private scope. Inference: allow observation while reserving invariant-changing writes for the owner.
captureService (Camera/Camera.swift:35) 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.
source (Camera/CameraPreview.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.
session (Camera/CameraPreview.swift:92) 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

Camera/Camera.swift:29 — representative boundary

@Observable
final class Camera {
    // ...
    private(set) var status = CameraStatus.unknown
    // ...
}

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 DocumentReaderApp The source’s App suffix makes this role explicit.
Receives callback-driven events PhotoCaptureDelegate The source’s Delegate suffix makes this role explicit.
Feature data or observable state VisionModel The source’s Model suffix makes this role explicit.
Framework-facing operations CaptureService The source’s Service suffix makes this role explicit.
User-interface presentation and input forwarding CameraView, ContactView, ContentView, ImageView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Protocol-oriented abstraction Camera/CameraPreview.swift:34 A local protocol and concrete conformance create an explicit capability boundary.
Delegate or data-source callbacks Camera/PhotoCapture.swift:70 Callback protocols invert event delivery back into the sample’s owner.
Service object Camera/CaptureService.swift:20 A role-named service contains framework-facing operations.
Binding-based state propagation Views/CameraView.swift:13 A binding exposes controlled read/write access while the upstream owner remains authoritative.
Publisher-backed observable state Camera/PhotoCapture.swift:27 Published properties notify observers while mutation remains with the state object.
Actor isolation Camera/CaptureService.swift:20 A declared actor creates an explicit isolation boundary; its executor is not described as a background thread.

Naming conventions

  • Types: App: DocumentReaderApp; Delegate: PhotoCaptureDelegate; Model: VisionModel; Service: CaptureService; View: CameraView, ContactView, ContentView, ImageView, PreviewView.
  • Protocols: PreviewSource, PreviewTarget.
  • Methods: makeUIView, updateUIView, setSession, connect, start, setUpSession, addInput, addOutput.
  • Files: DocumentReaderApp.swift, Camera/CameraPreview.swift, Camera/CaptureService.swift, Views/CameraView.swift, Views/ImageView.swift, VisionModel.swift.

Architecture takeaways

  • DocumentReaderApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, Vision, AVFoundation, AVKit 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.
  • Local protocol relationships provide an explicit substitution boundary.

Source map

Source file Relevant symbols
DocumentReaderApp.swift Cited implementation, DocumentReaderApp
DataModel.swift Cited implementation, Contact, TableCell, Content
Camera/CameraPreview.swift PreviewSource, Cited implementation, @MainActor, Task closure isolated to MainActor, Task, Sendable or @Sendable, AVFoundation, CameraPreview, PreviewView, PreviewTarget, DefaultPreviewSource
Camera/Camera.swift Cited implementation, async declaration or closure, @Observable, Camera, CameraStatus
Camera/PhotoCapture.swift Cited implementation, @Published, PhotoCaptureError, CaptureActivity, PhotoCapture, PhotoCaptureDelegate
Camera/CaptureService.swift CaptureService, Combine, CameraError
Views/CameraView.swift Cited implementation, SwiftUI state property wrapper, AVKit, CameraView, PhotoButtonStyle
BoundingBox.swift SwiftUI, Vision, BoundingBox
Views/ImageView.swift ImageView, RoundedButton
VisionModel.swift VisionModel, AppError
Views/ContactView.swift ContactView
Views/ContentView.swift ContentView