1---2name: solid-swift-23description: SOLID principles for Swift 6 and SwiftUI (iOS 26+). Files < 150 lines, protocols separated, @Observable, actors, Preview-driven development. Features Modular MANDATORY.4---5
6# SOLID Swift - Apple Best Practices 2026
7
8## Agent Workflow (MANDATORY)
9
10Before ANY implementation, use `TeamCreate` to spawn 3 agents:
11
121. **fuse-ai-pilot:explore-codebase** - Analyze existing architecture
132. **fuse-ai-pilot:research-expert** - Verify Swift/Apple docs via Apple Docs MCP + Context7
143. **XcodeBuildMCP** - Build validation after code changes. Then run **fuse-ai-pilot:sniper**.
15
16---
17
18## DRY - Reuse Before Creating (MANDATORY)
19
20**Before writing ANY new code:**
211. **Grep the codebase** for similar protocols, services, or logic
222. Check shared locations: `Core/Extensions/`, `Core/Utilities/`, `Core/Protocols/`
233. If similar code exists -> extend/reuse instead of duplicate
244. If code will be used by 2+ features -> create it in `Core/` directly
25
26---
27
28## Architecture (Features Modular MANDATORY)
29
30| Layer | Location | Max Lines |
31|-------|----------|-----------|
32| Views | `Features/[Feature]/Views/` | 80 |
33| ViewModels | `Features/[Feature]/ViewModels/` | 100 |
34| Services | `Features/[Feature]/Services/` | 100 |
35| Protocols | `Features/[Feature]/Protocols/` | 30 |
36| Shared | `Core/{Models,Protocols,Services,Extensions,Utilities}/` | - |
37
38**NEVER use flat `Sources/` structure - always `Features/[Feature]/`**
39
40---
41
42## Critical Rules (MANDATORY)
43
44| Rule | Value |
45|------|-------|
46| File limit | 150 lines (split at 120) |
47| ViewModels | `@MainActor @Observable` |
48| Protocols | `Features/[Feature]/Protocols/` or `Core/Protocols/` ONLY |
49| Models | `Sendable` structs |
50| Documentation | `///` on all public APIs |
51| Previews | Every View MUST have `#Preview` |
52
53---
54
55## Reference Guide
56
57### Concepts
58
59| Topic | Reference | When to consult |
60|-------|-----------|-----------------|
61| **SOLID Overview** | [solid-principles.md](references/solid-principles.md) | Quick reference all principles |
62| **SRP** | [single-responsibility.md](references/single-responsibility.md) | Fat views/VMs, splitting files |
63| **OCP** | [open-closed.md](references/open-closed.md) | Adding providers, extensibility |
64| **LSP** | [liskov-substitution.md](references/liskov-substitution.md) | Protocol contracts, testing |
65| **ISP** | [interface-segregation.md](references/interface-segregation.md) | Fat protocols, splitting |
66| **DIP** | [dependency-inversion.md](references/dependency-inversion.md) | Injection, testing, mocking |
67| **Concurrency** | [concurrency-patterns.md](references/concurrency-patterns.md) | Actors, @MainActor, Sendable |
68| **Anti-Patterns** | [anti-patterns.md](references/anti-patterns.md) | Code smells detection |
69
70### Templates
71
72| Template | When to use |
73|----------|-------------|
74| [view.md](references/templates/view.md) | SwiftUI View with subviews and #Preview |
75| [viewmodel.md](references/templates/viewmodel.md) | @Observable ViewModel with @MainActor |
76| [service.md](references/templates/service.md) | API Service, Mock, Cache actor |
77| [protocol.md](references/templates/protocol.md) | Service protocol, CQRS, Auth |
78| [model.md](references/templates/model.md) | Model, DTO, Error, Enum |
79
80---
81
82## Forbidden
83
84| Anti-Pattern | Fix |
85|--------------|-----|
86| Files > 150 lines | Split at 120 |
87| Protocols in impl files | Move to `Protocols/` directory |
88| `ObservableObject` | Use `@Observable` |
89| Completion handlers | Use `async/await` |
90| Missing `#Preview` | Add preview for every View |
91| Non-Sendable in async | Use `struct` with `let` |
92| Flat `Sources/` structure | Use `Features/[Feature]/` |