Swift Idioms and Patterns
Swift rewards value types, optionals, and protocol-oriented design. Idiomatic Swift = safe, expressive, Swifty.
Scope: Swift coding idioms. Test naming: GEMINI.md § Testing Strategy.
Value Types and Optionals
- Prefer structs over classes — value semantics by default. Classes only for identity, inheritance, or reference counting.
- Optionals — never force-unwrap (
!) in production:// ✅ Guard let for early exit guard let task = storage.findById(id) else { throw TaskError.notFound(id) } // ✅ Optional chaining let title = task?.title ?? "Untitled" // ❌ Force unwrap — crash risk let task = storage.findById(id)!
Error Handling
Typed throws (Swift 6) or
Errorprotocol:enum TaskError: Error { case notFound(String) case validationFailed(field: String, message: String) } func getTask(id: String) throws(TaskError) -> Task { ... }Resulttype for async callbacks:func fetchTask(id: String) async -> Result<Task, TaskError> { ... }
Concurrency
Structured concurrency with
async/await:func loadDashboard() async throws -> Dashboard { async let user = fetchUser(id) async let tasks = fetchTasks(userId: id) return Dashboard(user: try await user, tasks: try await tasks) }@Sendablefor closures crossing concurrency domains.Actors for thread-safe mutable state.
Naming (Swift API Design Guidelines)
- camelCase for functions, properties, variables.
- PascalCase for types, protocols, enums.
- Omit needless words —
remove(at:)notremoveItem(atIndex:). - Protocols for capabilities use
-able/-ible:Codable,Identifiable.
Testing
XCTest or Swift Testing (6.0+). Mock via protocols.
Formatting and Static Analysis
| Tool | Purpose | Command |
|---|---|---|
swift-format |
Formatting | swift-format -i -r Sources/ |
| SwiftLint | Linting | swiftlint lint --strict |
| Xcode Analyzer | Static analysis | Built-in |
Related
- Code Idioms and Conventions GEMINI.md § Code Idioms and Conventions
- Testing Strategy GEMINI.md § Testing Strategy
- Error Handling Principles GEMINI.md § Error Handling Principles