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 DocumentReaderApp → ContentView → VisionModel → CaptureService → Vision APIs. |
| Main patterns | Protocol-oriented abstraction, Delegate or data-source callbacks, Service object, Binding-based state propagation, Publisher-backed observable state, Actor-isolated state |
| Project style | 12 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
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
flowchart LR
N1["DocumentReaderApp"]
N2["ContentView"]
N3["VisionModel"]
N4["CaptureService"]
N5["Vision APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5
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
classDiagram
Contact *-- String : name
Contact *-- String : email
Contact *-- String : phoneNumber
TableCell o-- Content : content
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.
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: PreviewView → PreviewTarget, DefaultPreviewSource → PreviewSource.
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
private(set) var status = CameraStatus.unknown
/// An object that provides the connection between the capture session and the video preview layer.
var previewSource: PreviewSource { captureService.previewSource }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-isolated state | Camera/CaptureService.swift:20 |
Actor annotations make the concurrency ownership boundary explicit. |
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
DocumentReaderAppis 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 |
DocumentReaderApp |
DataModel.swift |
Contact, TableCell, Content |
Camera/CameraPreview.swift |
CameraPreview, PreviewView, PreviewSource, PreviewTarget, DefaultPreviewSource |
Camera/CaptureService.swift |
CameraError, CaptureService |
Views/CameraView.swift |
CameraView, PhotoButtonStyle |
Views/ImageView.swift |
ImageView, RoundedButton |
VisionModel.swift |
VisionModel, AppError |
Camera/PhotoCapture.swift |
PhotoCaptureError, CaptureActivity, PhotoCapture, PhotoCaptureDelegate |
Views/ContactView.swift |
ContactView |
Views/ContentView.swift |
ContentView |