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

Searching indexed content with natural language

At a glance

Item Summary
Purpose Give a language model access to your app’s Core Spotlight index to enable natural-language queries over searchable content.
App architecture A Swift sample with the source-visible chain LLMSearchUsingCoreSpotlightAppContentViewSessionViewModelCoreSpotlight APIs.
Main patterns Model-View-ViewModel, Delegate or data-source callbacks, Actor-isolated state
Project style 6 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
├── LLMSearchUsingCoreSpotlightApp/
│   ├── LLMSearchUsingCoreSpotlightApp.swift
│   ├── ContentView.swift
│   ├── Session.swift
│   ├── TrailDetailView.swift
│   ├── Indexer.swift
│   ├── Error+DisplayMessage.swift
│   ├── Config.xcconfig
│   └── SampleData.plist
├── Configuration/
│   └── SampleCode.xcconfig
├── LLMSearchUsingCoreSpotlight.entitlements
└── LLMSearchUsingCoreSpotlight.xcodeproj/
    ├── .xcodesamplecode.plist
    └── project.pbxproj

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 10 source declaration(s).

Overall architecture

Reference code

LLMSearchUsingCoreSpotlightApp/LLMSearchUsingCoreSpotlightApp.swift:10 — architecture anchor

@main
struct LLMSearchUsingCoreSpotlightApp: 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 Core Spotlight.

Ownership and state

Ownership evidence

LLMSearchUsingCoreSpotlightApp/ContentView.swift:13 — stored dependency or nearest verified ownership anchor

struct ContentView: View {
    // ...
    @State private var viewModel = SessionViewModel()
    // ...
}
Owner Object or state Relationship Mutation authority
ContentView SessionViewModel (viewModel) owns wrapper-managed state Owning lexical scope
ContentView Array (allTrails) owns wrapper-managed state Owning lexical scope
SummarySection String (response) owns value state Initialized by the owner; the binding is immutable
SummarySection Bool (isGenerating) 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

LLMSearchUsingCoreSpotlightApp/LLMSearchUsingCoreSpotlightApp.swift:11 — representative type boundary

struct LLMSearchUsingCoreSpotlightApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
Type Responsibility Depends on or conforms to
LLMSearchUsingCoreSpotlightApp Application entry and top-level composition App
ContentView User-interface presentation and input forwarding View
SearchingView User-interface presentation and input forwarding View
TrailImageView User-interface presentation and input forwarding View
TrailRowView User-interface presentation and input forwarding View
SessionViewModel UI-facing state and feature coordination Concrete collaborators/imported frameworks
TrailDetailView User-interface presentation and input forwarding View
SummarySection Represents a feature value or composable behavior View
TrailData Represents feature data Decodable
SpotlightIndexer Owns feature behavior and collaborator lifecycle NSObject, CSSearchableIndexDelegate

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
viewModel (LLMSearchUsingCoreSpotlightApp/ContentView.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.
allTrails (LLMSearchUsingCoreSpotlightApp/ContentView.swift:14) 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.
enrichedResults (LLMSearchUsingCoreSpotlightApp/ContentView.swift:79) 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.
searchResultsList (LLMSearchUsingCoreSpotlightApp/ContentView.swift:84) 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

LLMSearchUsingCoreSpotlightApp/ContentView.swift:13 — representative boundary

struct ContentView: View {
    // ...
    @State private var viewModel = SessionViewModel()
    // ...
}

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 LLMSearchUsingCoreSpotlightApp The source’s App suffix makes this role explicit.
User-interface presentation and input forwarding ContentView, SearchingView, TrailDetailView, TrailImageView The source’s View suffix makes this role explicit.
UI-facing state and feature coordination SessionViewModel The source’s ViewModel suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Model-View-ViewModel LLMSearchUsingCoreSpotlightApp/Session.swift:16 Role-named view models keep UI-facing state or coordination outside view declarations.
Delegate or data-source callbacks LLMSearchUsingCoreSpotlightApp/Indexer.swift:34 Callback protocols invert event delivery back into the sample’s owner.
Actor-isolated state LLMSearchUsingCoreSpotlightApp/Indexer.swift:96 Actor annotations make the concurrency ownership boundary explicit.

Naming conventions

  • Types: App: LLMSearchUsingCoreSpotlightApp; View: ContentView, SearchingView, TrailDetailView, TrailImageView, TrailRowView; ViewModel: SessionViewModel.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: trailList, reset, run, makeSpotlightTool, makeSession, listenForSearchResults, streamResponse, difficultyLabel.
  • Files: LLMSearchUsingCoreSpotlightApp/LLMSearchUsingCoreSpotlightApp.swift, LLMSearchUsingCoreSpotlightApp/ContentView.swift, LLMSearchUsingCoreSpotlightApp/TrailDetailView.swift.

Architecture takeaways

  • LLMSearchUsingCoreSpotlightApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches CoreSpotlight, SwiftUI, FoundationModels, 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
LLMSearchUsingCoreSpotlightApp/LLMSearchUsingCoreSpotlightApp.swift LLMSearchUsingCoreSpotlightApp
LLMSearchUsingCoreSpotlightApp/ContentView.swift ContentView, SummarySection, SearchingView, TrailImageView, TrailRowView
LLMSearchUsingCoreSpotlightApp/Session.swift SessionViewModel
LLMSearchUsingCoreSpotlightApp/TrailDetailView.swift TrailDetailView
LLMSearchUsingCoreSpotlightApp/Indexer.swift TrailData, SpotlightIndexer
LLMSearchUsingCoreSpotlightApp/Error+DisplayMessage.swift Feature implementation