Swift Project

Language: Swift October 1, 2025

Instruction file for Swift projects with iOS and macOS development.

---
applyTo: "**/*.swift"
---

# Swift Development Instructions (2025)

_Last updated: October 2025_

- Always use the fetch tool to look up the latest Swift documentation: https://docs.swift.org
- Use Swift 6 or later for new projects
- Leverage SwiftUI for building modern user interfaces
- Follow Swift API design guidelines for naming and conventions
- Use Swift Package Manager for dependency management
- Implement proper error handling with Result and throws

**Core Principles:**

- Use value types (structs) over reference types (classes) when possible
- Implement protocol-oriented programming
- Use optionals properly with optional chaining and nil coalescing
- Leverage Swift's type safety and type inference
- Use guard statements for early returns
- Follow naming conventions: camelCase for properties/methods, PascalCase for types

**SwiftUI:**

- Use SwiftUI for declarative UI development
- Implement proper state management with @State, @Binding, @ObservedObject
- Use @StateObject for object ownership
- Leverage @EnvironmentObject for dependency injection
- Use ViewModifiers for reusable styling
- Implement proper view composition

**Concurrency:**

- Use async/await for asynchronous operations (Swift 5.5+)
- Implement actors for thread-safe shared state
- Use @MainActor for UI updates
- Avoid completion handlers - prefer async functions
- Use Task for structured concurrency

**Memory Management:**

- Understand ARC (Automatic Reference Counting)
- Use weak and unowned references to break retain cycles
- Implement proper capture lists in closures
- Avoid retain cycles in delegation and closures

**Error Handling:**

- Use Result<Success, Failure> for operations that can fail
- Implement throwing functions with throws keyword
- Use do-catch for error handling
- Create custom error types conforming to Error protocol

**Testing:**

- Use XCTest for unit and UI testing
- Write tests with proper assertions and expectations
- Use test doubles (mocks, stubs) when needed
- Implement snapshot testing for UI validation

**Performance:**

- Use lazy properties for expensive computations
- Implement proper collection operations (map, filter, reduce)
- Use copy-on-write for efficient value semantics
- Profile with Instruments

**Summary:**

> For all Swift work, always use the fetch tool to look up the latest documentation from https://docs.swift.org. Build safe, fast, and expressive iOS/macOS applications.

Swift iOS macOS Mobile Programming Language