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
Project style 6 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: Task, await suspension point, @MainActor, Task closure isolated to MainActor, Sendable or @Sendable; none alone proves a background thread.
State/event model Source-visible mechanisms: SwiftUI state property wrapper, @Observable.
Key frameworks/packages CoreSpotlight, Foundation, SwiftUI, FoundationModels, Observation; these are source dependencies, not architecture labels.

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.

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
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. LLMSearchUsingCoreSpotlightApp/ContentView.swift:50
Suspension boundary await suspension point The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. LLMSearchUsingCoreSpotlightApp/ContentView.swift:52
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. LLMSearchUsingCoreSpotlightApp/Indexer.swift:96
Main isolation Task closure isolated to MainActor The cited operation explicitly enters a main-actor-isolated region. LLMSearchUsingCoreSpotlightApp/Indexer.swift:96
Transfer contract Sendable or @Sendable The source declares a sendability boundary; this alone does not synchronize mutable state. LLMSearchUsingCoreSpotlightApp/Indexer.swift:123

@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

LLMSearchUsingCoreSpotlightApp/ContentView.swift:50 — representative execution boundary

                Task {
                    withAnimation { viewModel.hasSearched = true }
                    await viewModel.run()
                }

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 SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. LLMSearchUsingCoreSpotlightApp/ContentView.swift:13
State propagation @Observable Observation macro publishes source-visible changes. LLMSearchUsingCoreSpotlightApp/Session.swift:15
Source import CoreSpotlight The cited file imports this module; runtime use and architectural role are not inferred. LLMSearchUsingCoreSpotlightApp/ContentView.swift:9
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. LLMSearchUsingCoreSpotlightApp/Error+DisplayMessage.swift:8
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. LLMSearchUsingCoreSpotlightApp/ContentView.swift:8
Source import FoundationModels The cited file imports this module; runtime use and architectural role are not inferred. LLMSearchUsingCoreSpotlightApp/Error+DisplayMessage.swift:9
Source import Observation The cited file imports this module; runtime use and architectural role are not inferred. LLMSearchUsingCoreSpotlightApp/Session.swift:11
Source import os The cited file imports this module; runtime use and architectural role are not inferred. LLMSearchUsingCoreSpotlightApp/Indexer.swift:11

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

LLMSearchUsingCoreSpotlightApp/LLMSearchUsingCoreSpotlightApp.swift:11 — representative type boundary

@main
struct LLMSearchUsingCoreSpotlightApp: App {
    // ...
            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.

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 Cited implementation, LLMSearchUsingCoreSpotlightApp
LLMSearchUsingCoreSpotlightApp/ContentView.swift Cited implementation, Task, await suspension point, SwiftUI state property wrapper, CoreSpotlight, SwiftUI, ContentView, SummarySection, SearchingView, TrailImageView, TrailRowView
LLMSearchUsingCoreSpotlightApp/Session.swift SessionViewModel, @Observable, Observation
LLMSearchUsingCoreSpotlightApp/Indexer.swift Cited implementation, @MainActor, Task closure isolated to MainActor, Sendable or @Sendable, os, TrailData, SpotlightIndexer
LLMSearchUsingCoreSpotlightApp/Error+DisplayMessage.swift Foundation, FoundationModels, Feature implementation
LLMSearchUsingCoreSpotlightApp/TrailDetailView.swift TrailDetailView