OmniscioGuidesCLAUDE.md examplesSwift
CLAUDE.md for a Swift project
Swift looks friendly to a code generator and is not. Concurrency is checked at compile time, the UI framework expects its state on one actor, and the formatter's lint mode passes by default. The file below writes down the parts a compiler cannot infer from the code alone.
Each block is explained underneath, and the examples hub has the same page for eight other stacks.
- The file, ready to copy.
- Why each block is there.
- What was left out, and why.
- Adapting it to a pure package, a macOS app or UIKit.
Checked against Anthropic's Claude Code documentation and Swift's and Apple's own documentation on November 25, 2026. By the Omniscio team. Updated .
The file
This one describes a walking-tracker app that records a route offline and uploads it later, with the model and storage code in a Swift package the app depends on. Save it as ./CLAUDE.md or ./.claude/CLAUDE.md in the repository root.
# Trailhead iOS walking tracker (SwiftUI, Swift 6). The app target lives in App/; models, rules and storage are the Swift package in Core/, which the app depends on. ## Commands swift build --package-path Core swift test --package-path Core swift test --package-path Core --filter DistanceTests swift format lint --strict --recursive Core/Sources Core/Tests swift format format -i -r Core/Sources Core/Tests ## Layout - App/ SwiftUI views, app lifecycle and nothing else - Core/Sources/ models, storage, upload rules - Core/Tests/ one test file per type in Core - AppTests/ UI tests that need a simulator and run from Xcode - Core/Package.swift the manifest of the package the app depends on ## Rules - Core/ imports Foundation only. No SwiftUI, no UIKit, no app types. - UI state lives on the main actor. Long or blocking work moves off it before it starts, not after the screen freezes. - await marks every suspension point. Never block a thread to wait for one. - A model is a Sendable value type. A reference type in Core needs a reason. - No force unwrap or try! outside tests and previews. - A public API reads clearly at the point where it is called; clarity beats brevity. - The Testing library is imported only in test targets, never in the app. ## Testing - Core/Tests uses Swift Testing: @Test for a test, #expect for an assertion. - Do not mix XCTest API and Swift Testing API in the same test, even though one target may hold both. - A test of the upload flow uses a stub transport, never the network. - A bug fix starts with a test that fails before the fix. - Most data races are compile errors under Swift 6. Fix the isolation; never switch the check off to make it build. ## Traps - `swift format lint` exits 0 with warnings unless you pass --strict. CI needs the flag; a bare lint proves nothing. - Importing Testing into the app target is not supported. It is a test library. - Running long work on the main actor is the default mistake: everything runs there until you move it off. - UI tests belong in the app's test target. Core/Tests runs without a simulator.
That is 51 lines. The layout stops at the five places that surprise a newcomer; everything else is discoverable from Package.swift.
Why each block is there
The first two lines
The opening names the platform, the language version and where the code actually lives. Swift's own getting-started page says Swift comes bundled with the Swift Package Manager, and the package documentation describes a package as a directory with a Package.swift manifest at its root, sources under Sources and tests under Tests. That layout is what makes the commands below unambiguous.
The command block
Anthropic asks for instructions concrete enough to verify. SwiftPM's own reference gives swift build the job of building sources into binaries and swift test the job of building and running tests, and describes --filter as running the test cases that match a regular expression, which is how one test is narrowed out of a suite.
The format pair is the part an agent gets wrong in both directions. swift-format's documentation describes lint as checking for style violations and printing diagnostics, and says lint warnings do not prevent a successful exit unless --strict is given. It also documents -i and -r, which are what make the second command rewrite a directory in place.
The layout
Five lines, and one rule that is invisible in the tree: Core/ imports Foundation and nothing UI. That is what keeps the package testable without a simulator, and it is the boundary an agent breaks first when a fix looks easier in a view.
Anthropic's size target keeps the list at five. A tree of the whole repository would spend the budget on something a session can read for itself.
The rules
The concurrency rules follow the Swift book's own explanation of actors. It says an actor protects access to mutable data by forcing code to take turns, that the main actor protects all the data used to show the UI, and that await marks the only places execution can suspend. A session that knows those three facts has most of what it needs to write code that compiles.
The public-API rule is quoted from the Swift API Design Guidelines, which open with clarity at the point of use as the most important goal and state that clarity is more important than brevity.
The Testing rule is a boundary, not a preference. Apple's documentation on defining tests says the library should only be imported into a test target or a library meant for test targets, and that importing it into a target intended for distribution is not supported or recommended.
Testing
The two framework lines are the documented shape: a test function is declared with the @Test attribute, #expect checks a condition and reports the expression when it fails, and a test target may contain both frameworks but a single test should not mix their API.
The last line tells an agent what the compiler is already doing: Swift's concurrency support detects and prevents data races, and most data races produce a compile-time error. Switching that check off to get a build is a decision for a human, not a passing test.
Traps
The lint line matters because the failure is silent: a bare swift format lint reports problems and still exits successfully, so a CI job that runs it without --strict is green and useless.
The main-actor line is the one a generated fix gets wrong most often. The Swift book says that before you start using concurrency everything runs on the main actor, and suggests moving long-running or resource-intensive code off it as you find it, which is the opposite of what a generated fix usually does.
What was left out, and why
- The dependency list. It is in
Package.swift, and a copy here is stale the first time somebody adds a package. - Formatting and lint configuration. It belongs in
.swift-format, where the tool enforces it. A second copy here can contradict the first, and Anthropic warns that contradicting instructions may be followed arbitrarily. - Signing, provisioning and release steps. A multi-step process belongs in a skill rather than in every session's context.
- A rule for one module. That is a path-scoped rule with
pathsfrontmatter, loaded only when Claude touches a matching file. - A simulator name or a personal team identifier. Those are
CLAUDE.local.md, which you add to.gitignore. - "Write idiomatic Swift." Uncheckable, and paid for on every turn.
Adapting it
Three swaps cover most Swift repositories.
- A pure package instead of an app. Delete the app, the UI rules and the UI-test line, and keep everything else. The commands are already the package's.
- A macOS app. Change the platform in the first line and keep the main-actor rules; they are the same on every Apple platform.
- UIKit or AppKit. Replace the SwiftUI line with your framework's, and keep the rule that
Core/imports no UI framework, which is what makes the move possible at all.
Then prove it loaded. /context all lists the memory files read at launch, and a file missing from that list is not being ignored; it is not being seen.
How this page was written
The guidance on what belongs in a CLAUDE.md, how long it should be and how Claude treats it was read from Anthropic's Claude Code memory documentation on November 25, 2026. The statements about Swift, SwiftPM, swift-format, Swift Testing and the concurrency model were read from swift.org and Apple's developer documentation the same day, and each one is linked in the Sources below.
The example file was written for this page and has not been run against a live project. Its rules are recommendations, not documented behaviour of Swift, and every command should be checked against your own package before you keep it.
Sources
- Anthropic's How Claude remembers your project, for what belongs in a CLAUDE.md, the size target, the concreteness rule and the conflict rule, read November 25, 2026.
- Swift — Getting started, for the toolchain and the bundled package manager, read November 25, 2026.
- Swift Package Manager, for the package layout and the
swift build,swift testand--filterentries, read November 25, 2026. - swift-format, for the
lintsubcommand,--strict,-iand-r, read November 25, 2026. - Apple — Defining test functions, for
@Testand where the library may be imported, read November 25, 2026. - Apple — Expectations, for
#expect, read November 25, 2026. - Apple — XCTest, for the XCTest framework and the warning about mixing it with Swift Testing, read November 25, 2026.
- The Swift Programming Language — Concurrency, for actors, the main actor,
awaitand data-race checking, read November 25, 2026. - Swift API Design Guidelines, for clarity at the point of use, read November 25, 2026.
Common questions
What should a CLAUDE.md for a Swift project contain?
The commands that build, test and lint the package, which code may touch the UI framework, the main-actor rule, and the traps: a lint that only warns unless you pass --strict, two test frameworks mixed in one test, and long work on the main actor. Anthropic's target is under 200 lines, so anything Claude can read from Package.swift stays out.
How do I run one Swift test?
swift test --filter, with an expression that matches the test's name. Swift's package documentation lists that option as running the test cases that match a regular expression; in practice that means a full type and method name, or a shorter fragment that matches only one test.
Should the file mention XCTest or Swift Testing?
Name the one your test target uses, because mixing them in a single test is what the documentation warns against: a test target can contain both, but API from the two frameworks should not be mixed in the same test. Apple also says the Testing library belongs only in a test target or a library meant for test targets, never in the app target.
Does a swift-format lint failure stop the build?
Not by itself. swift-format's own documentation says lint warnings do not prevent a successful exit unless you pass --strict, which turns lint warnings into a non-zero exit code. A CI gate therefore needs the flag, and the CLAUDE.md should name the command with the flag rather than the bare one.
Write CLAUDE.md once, and every agent reads it
Omniscio keeps your canonical CLAUDE.md or AGENTS.md mirrored to the filenames Codex, Gemini, Cursor and Copilot look for, and flags a copy that was edited outside it. Any model, any harness.
The features page covers what else it does, pricing covers the plans, and you can download it for Windows, macOS or Linux.