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

Book Tracker: Using Evaluations to evaluate an intelligent feature

At a glance

Item Summary
Purpose Measure and improve the quality of your app’s intelligence-powered features using the Evaluations framework.
App architecture A Swift sample bundle with entry-bearing project variants BookTracker, DatasetExtractor, each leading to Evaluations APIs.
Main patterns Service object, Binding-based state propagation, Actor-isolated state
Project style 20 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
├── DatasetExtractor/
│   └── main.swift
├── BookTracker/
│   ├── BookTrackerApp.swift
│   ├── Services/
│   │   ├── BookSearchTools.swift
│   │   └── BookTaggingService.swift
│   ├── Views/
│   │   ├── BookDetailView.swift
│   │   ├── LibraryView.swift
│   │   ├── SearchView.swift
│   │   └── AddBookView.swift
│   └── ContentView.swift
├── BookSampleGenerator/
│   └── main.swift
├── HillClimbingEvaluations/
│   └── ModelJudgeAlignmentEvaluation.swift
└── BookTrackerEvaluations/
    └── SearchBooks.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 47 source declaration(s).

Overall architecture

Reference code

DatasetExtractor/main.swift:165 — architecture anchor

// @main cannot be used in main.swift — Swift's implicit top-level entry point and
// @main are mutually exclusive. Calling .main() explicitly is the equivalent.
DatasetExtractorCommand.main()

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

Ownership evidence

BookTracker/BookTrackerApp.swift:14 — stored dependency or nearest verified ownership anchor

    let container: ModelContainer = {
        // ...
    }()
Owner Object or state Relationship Mutation authority
BookTrackerApp ModelContainer (container) stores or receives Initialized by the owner; the binding is immutable
BookSearchCollector Array (orderedIDs) owns value state Owning lexical scope
BookSearchCollector Set (seen) owns value state Owning lexical scope
BookSnapshot String (id) owns value state 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

BookTracker/BookTrackerApp.swift:13 — representative type boundary

struct BookTrackerApp: App {
    // ...
}
Type Responsibility Depends on or conforms to
BookTrackerApp Application entry and top-level composition App
DatasetExtractorCommand Represents or runs a user/system command ParsableCommand
BookDetailView User-interface presentation and input forwarding View
BookTagsView User-interface presentation and input forwarding View
BookTaggingService Framework-facing operations Concrete collaborators/imported frameworks
ContentView User-interface presentation and input forwarding View
LibraryView User-interface presentation and input forwarding View
SearchView User-interface presentation and input forwarding View
AddBookView User-interface presentation and input forwarding View
ResultKey Defines a closed set of feature states or choices 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
addSampleBooks (BookTracker/BookTrackerApp.swift:34) 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.
books (BookTracker/ContentView.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.
modelContext (BookTracker/ContentView.swift:41) 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.
orderedIDs (BookTracker/Services/BookSearchTools.swift:25) 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

BookTracker/BookTrackerApp.swift:34 — representative boundary

private func addSampleBooks(in context: ModelContext) {
    // ...
}

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 BookTrackerApp The source’s App suffix makes this role explicit.
Represents or runs a user/system command DatasetExtractorCommand The source’s Command suffix makes this role explicit.
Framework-facing operations BookTaggingService The source’s Service suffix makes this role explicit.
User-interface presentation and input forwarding AddBookView, BookDetailView, BookTagsView, ContentView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Service object BookTracker/Services/BookTaggingService.swift:20 A role-named service contains framework-facing operations.
Binding-based state propagation BookTracker/Views/BookDetailView.swift:206 A binding exposes controlled read/write access while the upstream owner remains authoritative.
Actor-isolated state BookTracker/Services/BookSearchTools.swift:24 Actor annotations make the concurrency ownership boundary explicit.

Naming conventions

  • Types: App: BookTrackerApp; Command: DatasetExtractorCommand; Service: BookTaggingService; View: AddBookView, BookDetailView, BookTagsView, ContentView, LibraryView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: run, extractPairs, extractPrompt, resolvedOutputURL, defaultOutputURL, addSampleBooks, add, call.
  • Files: BookTracker/BookTrackerApp.swift, BookTracker/Views/BookDetailView.swift, BookTracker/Services/BookTaggingService.swift, BookTracker/ContentView.swift, BookTracker/Views/LibraryView.swift, BookTracker/Views/SearchView.swift.

Architecture takeaways

  • main is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, FoundationModels, SwiftData, Evaluations 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
DatasetExtractor/main.swift ResultKey, InputKey, OutputKey, DatasetExtractorCommand
BookTracker/BookTrackerApp.swift BookTrackerApp
BookTracker/Services/BookSearchTools.swift BookAssistant, BookSearchCollector, BookSnapshot, SearchBooksArguments, GetBookDetailsArguments, FindSimilarBooksArguments, SearchBooksTool, GetBookDetailsTool, FindSimilarBooksTool, BookDetailsPayload
BookSampleGenerator/main.swift Feature implementation
BookTracker/Views/BookDetailView.swift BookDetailView, BookDetailHeader, BookReview, BookTagsView
BookTracker/Services/BookTaggingService.swift BookTags, BookTaggingService
BookTracker/ContentView.swift ContentView, BookDetailDestination
BookTracker/Views/LibraryView.swift LibraryView, BookCover
BookTracker/Views/SearchView.swift SearchView, BookRow
HillClimbingEvaluations/ModelJudgeAlignmentEvaluation.swift BundleToken, BookTagJudgmentValue, CodingKeys, BookTagJudgmentCalibration, BookTagJudgmentCalibrationTests