Swift Standards
Swift Standards
Section titled “Swift Standards”1. Package Management
Section titled “1. Package Management”- Tool: Swift Package Manager (SwiftPM).
Package.swiftfor dependencies. - Version: Swift 6.0+ (
// swift-tools-version: 6.0). Use the latest stable toolchain; the Swift 6 language mode with strict concurrency is the baseline (see §8). - Dependencies: Declare in
Package.swift. Pin versions or use ranges. - Lockfile: Commit
Package.resolvedfor apps, executables, and anything with a CI build. Libraries consumed by other packages may omit it. Enforced bylint-checks/swift/package-resolved. - Age Gate: Do not adopt any Swift package version (tag/release) published less than 3 days ago. Verify the GitHub release date before upgrading. See
sec-01§5 for exceptions.
2. Code Style
Section titled “2. Code Style”- Formatter + Linter (default gate):
swift-format. It ships with the Swift 6 toolchain (swift format), so every contributor and CI runner has it without a Homebrew install. Configure via.swift-format(JSON); line length 120. CI runsswift format lint --strict --recursive Sources Testsand fails on any diagnostic. Enforced bylint-checks/swift/swift-format-config. - SwiftLint (optional): Add
swiftlintonly for rules swift-format does not cover (e.g.force_unwrapping,todo). It is not bundled with Xcode, so it must never be the only formatting gate — a project with.swiftlint.ymlbut no.swift-formatis non-compliant. - Warnings as errors: Build with warnings as errors in CI, not just in release. For SwiftPM:
swift build -Xswiftc -warnings-as-errorsandswift test -Xswiftc -warnings-as-errors, or.treatAllWarnings(as: .error)inswiftSettingson tools 6.2+. For Xcode targets:SWIFT_TREAT_WARNINGS_AS_ERRORS: YESinproject.yml. Enforced bylint-checks/swift/warnings-as-errors.
Package.swift Example
Section titled “Package.swift Example”import PackageDescription
let package = Package( name: "ProjectName", platforms: [.macOS(.v14), .iOS(.v17)], dependencies: [ .package(url: "https://github.com/apple/swift-log", from: "1.6.0") ], targets: [ .target( name: "Domain", swiftSettings: [ // Swift 6 language mode == strict concurrency. Do not downgrade // with -strict-concurrency=minimal; fix the diagnostics instead. .swiftLanguageMode(.v6) ] ) ]).swift-format Example
Section titled “.swift-format Example”{ "version": 1, "lineLength": 120, "indentation": { "spaces": 4 }, "respectsExistingLineBreaks": true, "rules": { "NeverForceUnwrap": true, "NeverUseImplicitlyUnwrappedOptionals": true }}3. Naming Conventions
Section titled “3. Naming Conventions”- Types/Protocols:
PascalCase - Functions/Variables:
camelCase - Constants:
camelCase(useletfor immutability) - Private:
privateaccess control (no underscore prefix) - Files:
PascalCase.swiftmatching primary type name
4. Project Structure
Section titled “4. Project Structure”Sources/├── Domain/│ ├── Entities/│ └── ValueObjects/├── Application/│ ├── UseCases/│ └── Interfaces/└── Infrastructure/ └── Repositories/Tests/└── [Mirror Sources structure]5. Language Features
Section titled “5. Language Features”- Immutability: Prefer
letovervar. Usestructfor value types. - Optionals: Use
Optional<T>(T?) for nullable values. Avoid force unwrapping (!). - Protocols: Use protocols for interfaces. Prefer protocol-oriented design.
- Generics: Use generics for reusable code. Use
whereclauses for constraints.
Example
Section titled “Example”struct Email: Equatable { let value: String
init(_ value: String) throws { guard value.contains("@") && value.contains(".") else { throw InvalidEmailError(value) } self.value = value }}
enum Result<T> { case success(T) case failure(Error)}6. Error Handling
Section titled “6. Error Handling”- Errors: Use
Errorprotocol. Preferenumfor typed errors. - Throwing: Use
throwsfor functions that can fail. Usetry?ordo-catch. - Result Type: Use
Result<T, Error>for functional error handling.
enum DomainError: Error { case invalidEmail(String) case userNotFound(String)
var localizedDescription: String { switch self { case .invalidEmail(let email): return "Invalid email: \(email)" case .userNotFound(let id): return "User not found: \(id)" } }}7. Testing
Section titled “7. Testing”- Framework:
XCTest(XCTAssert*) or Swift Testing (import Testing,@Test,#expect). Pick one per package; both run underswift test. - Mocking: Use protocols for testability. Create test doubles manually or use
Mockingbird. - Coverage:
swift test --enable-code-coverage(or Xcode coverage). 95% is the absolute minimum for any module. Target 100% for domain, 95%+ for application and infrastructure. - Hardware-dependent tests skip explicitly. A GPU/Metal test on a host without a Metal device (Linux CI, some virtualized macOS runners) must skip with a reason — never silently pass, and never guard the assertions with
if let device. Skipped tests show up in the report; a hollow pass does not.
import XCTest#if canImport(Metal)import Metal#endif
final class RendererTests: XCTestCase { override func setUpWithError() throws { #if canImport(Metal) // Skip, don't pass: the report shows "skipped (No Metal device…)". try XCTSkipUnless(MTLCreateSystemDefaultDevice() != nil, "No Metal device on this host") #else // Linux CI: Metal is not even importable. Still a recorded skip, never a build failure. throw XCTSkip("Metal is unavailable on this platform") #endif }
#if canImport(Metal) func testShouldRenderTriangle() throws { let device = try XCTUnwrap(MTLCreateSystemDefaultDevice()) let renderer = Renderer(device: device) XCTAssertEqual(try renderer.render(.triangle).vertexCount, 3) } #endif}
// Swift Testing equivalent#if canImport(Metal)@Test(.enabled(if: MTLCreateSystemDefaultDevice() != nil, "No Metal device on this host"))func rendersTriangle() { /* ... */ }#else@Test(.disabled("Metal is unavailable on this platform"))func rendersTriangle() {}#endifTest Structure
Section titled “Test Structure”import XCTest@testable import Domain
final class UserTests: XCTestCase { func testShouldCreateUserWithValidEmail() throws { // Given
// When let user = User(email: email, name: "Test User")
// Then }}8. Concurrency (Swift 6 strict concurrency baseline)
Section titled “8. Concurrency (Swift 6 strict concurrency baseline)”- Language mode: Every target compiles in Swift 6 language mode (
.swiftLanguageMode(.v6)), which makes data-race safety a compile error. Do not opt a target down to-strict-concurrency=minimalortargetedto silence diagnostics; fix the isolation instead.@preconcurrency importis acceptable only for third-party modules that have not adoptedSendable, with a comment naming the upstream issue. - Async/Await: Use
async/awaitfor concurrent operations. - Actors: Use
actorfor thread-safe mutable state. UI state is@MainActor. Mark reference types that cross isolation boundariesfinalandSendable, or keep them inside an actor. - Tasks: Use
Task,TaskGroup, andasync letfor structured concurrency. UnstructuredTask { }needs an owner that cancels it. - Sendable closures: Closures handed to another isolation domain must be
@Sendable; capture onlySendablevalues.
actor UserRepository { private var users: [String: User] = [:]
func findById(_ id: String) -> User? { users[id] }
func save(_ user: User) { users[user.id] = user }}8.1 Actor-owned opaque handles (C, Metal, GPU, file descriptors)
Section titled “8.1 Actor-owned opaque handles (C, Metal, GPU, file descriptors)”Opaque handles from C libraries are not Sendable, and an actor’s deinit is nonisolated — it cannot touch actor-isolated state to release the handle. The pattern that satisfies both strict concurrency and deterministic cleanup:
- Own the handle inside a
final classguarded byOSAllocatedUnfairLock(Apple platforms, iOS 16+/macOS 13+) or aMutex(Swift 6Synchronization, iOS 18+/macOS 15+). Raw pointers are notSendable, so the wrapper is declared@unchecked Sendable— and the lock is the only acceptable justification for that attribute. Every@unchecked Sendablein the codebase names the lock that guards it in a comment; one without a lock is a review blocker. - Release in the wrapper’s
deinit, through the lock. Anonisolated deinitmay call into the lock; it may not call actor-isolated methods. - Let an
actorown the wrapper and expose a typed API. Callers never see the raw pointer. - Copy before release at the C boundary. When C returns a buffer that is freed on the next call or on handle release, copy it into a Swift-owned
[UInt8]/Databefore releasing the lock or the handle. Never hand a Swift buffer to C past the call that borrows it (withUnsafeBytesscope ends, the pointer is dead).
import os
/// `@unchecked` is justified solely by `state`: every access to the pointer goes through the lock.final class MeshHandle: @unchecked Sendable { private let state: OSAllocatedUnfairLock<OpaquePointer?>
init(_ raw: OpaquePointer) { state = OSAllocatedUnfairLock(initialState: raw) }
/// Copy-before-release: the C buffer is only valid while we hold the lock. func vertices() -> [Float] { state.withLock { raw -> [Float] in guard let raw else { return [] } var count: Int32 = 0 let ptr = mesh_vertices(raw, &count) // borrowed, freed by C on next call return Array(UnsafeBufferPointer(start: ptr, count: Int(count))) } }
deinit { // nonisolated by definition; safe because the lock, not an actor, guards the pointer. state.withLock { raw in if let raw { mesh_free(raw) } raw = nil } }}
actor MeshStore { private var meshes: [UUID: MeshHandle] = [:]
func load(_ bytes: [UInt8]) throws -> UUID { let raw = try bytes.withUnsafeBufferPointer { buf -> OpaquePointer in guard let p = mesh_parse(buf.baseAddress, Int32(buf.count)) else { throw MeshError.parse } return p } let id = UUID() meshes[id] = MeshHandle(raw) return id }}9. Memory Management
Section titled “9. Memory Management”- ARC: Automatic Reference Counting. Avoid retain cycles with
weak/unowned. - Closures: Prefer structured concurrency (
async let,TaskGroup) over escaping closures — structured tasks end with their scope and cannot form cycles. Use[weak self]in long-lived escaping closures (delegates, observers,Timer, Combine sinks);[unowned self]only when the closure provably cannot outliveself. - Value Types: Prefer
structoverclasswhen possible. - Native handles: See §8.1 — wrap every C/Metal/file-descriptor handle so release is deterministic and copy-before-release is the only way out of the C boundary.
10. Dependencies
Section titled “10. Dependencies”Common Libraries
Section titled “Common Libraries”- Networking:
URLSession(native),Alamofirefor complex needs - JSON:
Codableprotocol (native),SwiftyJSONfor dynamic parsing - Logging:
swift-log(server-side),os.log(Apple platforms)
11. Documentation
Section titled “11. Documentation”- Doc Comments: Use triple-slash (
///) for documentation. Supports Markdown. - Format: Use
- Parameter,- Returns,- Throwstags.
/// Creates a new user with validated email address.////// - Parameter email: Valid email address (must match RFC 5322)/// - Parameter name: User's full name (non-null, non-empty)/// - Returns: New User entity instance/// - Throws: `InvalidEmailError` if email format is invalidfunc createUser(email: String, name: String) throws -> User { // Implementation}12. Platform-Specific
Section titled “12. Platform-Specific”- iOS/macOS: Follow Apple’s Human Interface Guidelines.
- Server-Side: Use Vapor or SwiftNIO for HTTP servers.
- Cross-Platform: Use
#if os()for platform-specific code.
13. Security
Section titled “13. Security”Full security standards:
standards/security/sec-01_security_standards.md
- SAST: Use Xcode’s built-in static analyzer. No major third-party SAST tool for Swift.
- Dependency scanning: Enable GitHub Dependabot for SPM dependencies.
- Banned functions: See sec-01_security_standards.md for the complete banned-functions list with language-specific examples.
- Secure random: Use
SystemRandomNumberGeneratororSecRandomCopyBytesfor security contexts.
14. Build & Project Generation (Apple platforms)
Section titled “14. Build & Project Generation (Apple platforms)”For app targets (iOS/macOS/tvOS/watchOS/visionOS), the .xcodeproj is a build artifact, not a source file. Treat a declarative manifest as the source of truth, and set code-signing build settings deliberately so device builds work the first time. There are two compliant paths; pick one per app and record the choice in an ADR.
14.0 Choosing the build path: SwiftPM-only vs. generated project
Section titled “14.0 Choosing the build path: SwiftPM-only vs. generated project”SwiftPM-only (Package.swift is the whole manifest) |
Generated project (XcodeGen project.yml / Tuist Project.swift) |
|
|---|---|---|
| Use when | macOS apps, CLI tools, SwiftUI apps with no asset catalog or entitlements, libraries, plugins | iOS/visionOS apps needing asset catalogs, entitlements, app extensions, multi-target signing, Interface Builder |
| Checked in | Package.swift, Package.resolved |
project.yml, Package.resolved; .xcodeproj gitignored |
| CI build | swift build -c release -Xswiftc -warnings-as-errors; swift test |
xcodegen generate && xcodebuild -scheme … test |
| Device run / signing | xcodebuild -scheme <Target> -destination … directly against Package.swift (Xcode synthesizes the project in memory) |
Per §14.2 |
| Formatting gate | swift format lint --strict |
swift format lint --strict |
SwiftPM-only rules:
- There is no
.xcodeprojin the repository and none is generated into it. OpeningPackage.swiftin Xcode is the IDE path; the CLI path isswift build/swift test, and both must stay green. - Executable app targets declare their resources with
resources: [.process("Resources")]and read them throughBundle.module. - An
Info.plistor entitlements that an executable needs are passed vialinkerSettings: [.unsafeFlags(["-Xlinker", "-sectcreate", …])]only in the root package (SwiftPM rejectsunsafeFlagsin dependencies). If the app grows past whatunsafeFlagscan express cleanly, switch to the generated-project path — do not hand-create an.xcodeproj. - Signing for distribution happens outside the manifest (
codesign/notarytoolin the release lane), never by checking in a project file.
14.1 Project file generation
Section titled “14.1 Project file generation”- Tool: Use XcodeGen (
project.yml) or Tuist (Project.swift) for any project with more than one target. Hand-maintained.xcodeprojfiles cause merge conflicts and silent setting drift. - Source of truth:
project.yml(orProject.swift) is checked in; the generated.xcodeprojis gitignored. Never edit build settings in the Xcode UI — regeneration overwrites them. - Regenerate before build: CI and local
make buildtargets must runxcodegen generate(ortuist generate) before invokingxcodebuild, so the project file always matches the spec.
14.2 Code signing — the rules
Section titled “14.2 Code signing — the rules”These settings are easy to get wrong, and the failure modes are confusing (UI looks correct, device install still fails). Follow these rules:
-
Set
DEVELOPMENT_TEAMto the 10-character Apple Developer Team ID. Team IDs are public identifiers (visible in App Store listings); safe to commit. WithCODE_SIGN_STYLE: Automatic(the recommended default), an emptyDEVELOPMENT_TEAMprevents Xcode from resolving a signing identity and the build product ends up unsigned. Manual signing (explicitPROVISIONING_PROFILE_SPECIFIER/CODE_SIGN_IDENTITY) can still produce a signed device build withoutDEVELOPMENT_TEAM, but if you’re using Automatic signing, set it. -
Use
CODE_SIGN_STYLE: Automaticfor app targets unless you have a specific reason to manage profiles manually (e.g., enterprise distribution). Automatic signing handles simulator and device correctly with a valid team. -
Never set
CODE_SIGNING_ALLOWED: NOorCODE_SIGNING_REQUIRED: NOat thebasesettings level. Base settings apply to every configuration and every destination — including device builds, where they produce an unsigned binary that iOS rejects with a “code is unsigned” error at install time. The Xcode UI will still show “Automatic” signing while the build silently skips it. -
If a CI lane genuinely needs unsigned simulator builds (e.g., a hosted runner without keychain access), scope the override to that configuration only — never the base — and document why:
# project.yml — XcodeGen exampleconfigs:Debug: debugRelease: releaseDebugCI: debug # CI-only config, simulator builds without keychainsettings:base:DEVELOPMENT_TEAM: ABCDE12345CODE_SIGN_STYLE: Automaticconfigs:DebugCI:CODE_SIGNING_ALLOWED: NOCODE_SIGNING_REQUIRED: NOPrefer the simpler path: provision the CI runner with a signing identity (e.g., via
apple-actions/import-codesign-certs) and keep one signing posture across all configurations.
14.3 Minimal correct app-target spec
Section titled “14.3 Minimal correct app-target spec”settings: base: SWIFT_VERSION: "<latest stable>" # match the toolchain (Xcode → Swift version) DEVELOPMENT_TEAM: ABCDE12345 # 10-char Apple Developer Team ID CODE_SIGN_STYLE: Automatic ENABLE_USER_SCRIPT_SANDBOXING: YEStargets: MyApp: type: application platform: iOS settings: base: PRODUCT_BUNDLE_IDENTIFIER: com.example.myapp14.4 Diagnostic checklist for “code is unsigned” / device-install failures
Section titled “14.4 Diagnostic checklist for “code is unsigned” / device-install failures”When a build looks correctly configured in Xcode but the device rejects it:
- Inspect the generated
.xcodeproj, not the UI. Runfind . -name project.pbxproj -path '*.xcodeproj/*' -exec grep -HE 'DEVELOPMENT_TEAM|CODE_SIGN' {} +from the repo root — verifyDEVELOPMENT_TEAMis set and noCODE_SIGNING_ALLOWED = NOlines appear. (Avoid relying on a bare*.xcodeproj/project.pbxprojglob: it fails silently on nested layouts and on shells that error when the glob has zero matches.) - Confirm
xcodegen generateran since the last spec change. UI edits without a correspondingproject.ymlchange get overwritten on regen. - Check
basevs. per-config settings. ANOat base overrides any device-specific config. - Run
codesign -dvv <path-to-built-.app>on the build product. “code object is not signed at all” confirms the binary is unsigned regardless of UI state.
14.5 Re-enabling signing surfaces latent target-config gaps
Section titled “14.5 Re-enabling signing surfaces latent target-config gaps”Disabling signing at base doesn’t only break device installs — it also masks per-target configuration that the build system would otherwise reject. Common gap: a unit-test or extension target with no Info.plist and no GENERATE_INFOPLIST_FILE: YES. The signing pass needs a plist to embed the signature; with signing turned off it never runs the check, and the build silently succeeds. The moment signing is re-enabled, xcodebuild fails with:
Cannot code sign because the target does not have an Info.plist file and one is not being generated automatically.
Implication: when re-enabling signing on an existing project, run xcodebuild test (not just xcodebuild build) against every scheme — every target that participates in the build must independently satisfy signing prerequisites. For test bundles and other auxiliary targets that don’t need a hand-authored plist, set GENERATE_INFOPLIST_FILE: YES on the target’s settings:
targets: MyAppTests: type: bundle.unit-test platform: iOS settings: base: GENERATE_INFOPLIST_FILE: YES # required once signing is enabled BUNDLE_LOADER: $(TEST_HOST) TEST_HOST: $(BUILT_PRODUCTS_DIR)/MyApp.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/MyApp15. Build Caches & Cloud-Synced Checkouts
Section titled “15. Build Caches & Cloud-Synced Checkouts”SwiftPM and Xcode write large, churny caches next to the checkout: .build/ (SwiftPM), .swiftpm/ (per-package Xcode state), and DerivedData/ (Xcode). When the checkout lives inside an iCloud Drive, Dropbox, OneDrive, or Google Drive folder, the sync client fights the build system — files vanish mid-compile, .o files get .icloud placeholders, and incremental builds stall for minutes while thousands of objects upload.
- Checkouts live outside synced folders. Clone into
~/src,~/code, or another path the sync client does not watch. If the project must live in a synced folder, mark the cache directories as excluded (.nosyncsuffix on macOS iCloud, Dropbox/OneDrive selective sync) — but the simpler rule is to move the checkout. - Redirect caches when you cannot move the checkout.
swift build --scratch-path ~/.cache/swiftpm/<project>andxcodebuild -derivedDataPath ~/Library/Developer/Xcode/DerivedData/<project>keep artifacts out of the synced tree. Put the flags in theMakefileso every lane uses them. - Gitignore the caches.
.build/,.swiftpm/,DerivedData/,*.xcuserstate, andxcuserdata/are never committed.Package.resolvedis committed (§1). - CI caches by lockfile hash. Key the
.build/cache onhashFiles('Package.resolved')so a dependency bump invalidates it and nothing else does.