solid-swift
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSOLID Swift - Apple Best Practices 2026
SOLID Swift - Apple 2026最佳实践
Agent Workflow (MANDATORY)
Agent工作流(强制要求)
Before ANY implementation, use to spawn 3 agents:
TeamCreate- fuse-ai-pilot:explore-codebase - Analyze existing architecture
- fuse-ai-pilot:research-expert - Verify Swift/Apple docs via Apple Docs MCP + Context7
- XcodeBuildMCP - Build validation after code changes. Then run fuse-ai-pilot:sniper.
在进行任何实现之前,使用生成3个Agent:
TeamCreate- fuse-ai-pilot:explore-codebase - 分析现有架构
- fuse-ai-pilot:research-expert - 通过Apple Docs MCP + Context7验证Swift/Apple文档
- XcodeBuildMCP - 代码变更后进行构建验证。然后运行fuse-ai-pilot:sniper。
DRY - Reuse Before Creating (MANDATORY)
DRY原则 - 优先复用而非创建(强制要求)
Before writing ANY new code:
- Grep the codebase for similar protocols, services, or logic
- Check shared locations: ,
Core/Extensions/,Core/Utilities/Core/Protocols/ - If similar code exists -> extend/reuse instead of duplicate
- If code will be used by 2+ features -> create it in directly
Core/
在编写任何新代码之前:
- 在代码库中搜索类似的协议、服务或逻辑
- 检查共享目录:、
Core/Extensions/、Core/Utilities/Core/Protocols/ - 如果存在类似代码 -> 扩展/复用而非重复编写
- 如果代码将被2个及以上功能使用 -> 直接在中创建
Core/
Architecture (Features Modular MANDATORY)
架构设计(功能模块化是强制要求)
| Layer | Location | Max Lines |
|---|---|---|
| Views | | 80 |
| ViewModels | | 100 |
| Services | | 100 |
| Protocols | | 30 |
| Shared | | - |
NEVER use flat structure - always
Sources/Features/[Feature]/| 层级 | 位置 | 最大行数 |
|---|---|---|
| 视图 | | 80 |
| 视图模型 | | 100 |
| 服务 | | 100 |
| 协议 | | 30 |
| 共享代码 | | - |
禁止使用扁平的结构 - 必须使用结构
Sources/Features/[Feature]/Critical Rules (MANDATORY)
关键规则(强制要求)
| Rule | Value |
|---|---|
| File limit | 150 lines (split at 120) |
| ViewModels | |
| Protocols | |
| Models | |
| Documentation | |
| Previews | Every View MUST have |
| 规则 | 要求 |
|---|---|
| 文件行数限制 | 150行(120行时就需要拆分) |
| 视图模型 | 使用 |
| 协议 | 只能放在 |
| 模型 | 采用 |
| 文档 | 所有公开API都需要添加 |
| 预览 | 每个视图必须包含 |
Reference Guide
参考指南
Concepts
核心概念
| Topic | Reference | When to consult |
|---|---|---|
| SOLID Overview | solid-principles.md | Quick reference all principles |
| SRP | single-responsibility.md | Fat views/VMs, splitting files |
| OCP | open-closed.md | Adding providers, extensibility |
| LSP | liskov-substitution.md | Protocol contracts, testing |
| ISP | interface-segregation.md | Fat protocols, splitting |
| DIP | dependency-inversion.md | Injection, testing, mocking |
| Concurrency | concurrency-patterns.md | Actors, @MainActor, Sendable |
| Anti-Patterns | anti-patterns.md | Code smells detection |
| 主题 | 参考文档 | 适用场景 |
|---|---|---|
| SOLID总览 | solid-principles.md | 快速查阅所有原则 |
| 单一职责原则(SRP) | single-responsibility.md | 视图/视图模型过于臃肿、拆分文件时 |
| 开闭原则(OCP) | open-closed.md | 添加服务提供者、实现扩展性时 |
| 里氏替换原则(LSP) | liskov-substitution.md | 协议契约、测试时 |
| 接口隔离原则(ISP) | interface-segregation.md | 协议过于臃肿、拆分协议时 |
| 依赖倒置原则(DIP) | dependency-inversion.md | 依赖注入、测试、模拟对象时 |
| 并发处理 | concurrency-patterns.md | 使用Actors、@MainActor、Sendable时 |
| 反模式 | anti-patterns.md | 检测代码异味时 |
Templates
模板
| Template | When to use |
|---|---|
| view.md | SwiftUI View with subviews and #Preview |
| viewmodel.md | @Observable ViewModel with @MainActor |
| service.md | API Service, Mock, Cache actor |
| protocol.md | Service protocol, CQRS, Auth |
| model.md | Model, DTO, Error, Enum |
| 模板 | 适用场景 |
|---|---|
| view.md | 包含子视图和#Preview的SwiftUI视图 |
| viewmodel.md | 带有@MainActor的@Observable视图模型 |
| service.md | API服务、模拟对象、缓存Actor |
| protocol.md | 服务协议、CQRS、认证相关 |
| model.md | 模型、DTO、错误类型、枚举 |
Forbidden
禁止事项
| Anti-Pattern | Fix |
|---|---|
| Files > 150 lines | Split at 120 |
| Protocols in impl files | Move to |
| Use |
| Completion handlers | Use |
Missing | Add preview for every View |
| Non-Sendable in async | Use |
Flat | Use |
| 反模式 | 修复方案 |
|---|---|
| 文件行数>150行 | 在120行时拆分文件 |
| 协议放在实现文件中 | 移动到 |
使用 | 改用 |
| 使用完成回调 | 改用 |
缺少 | 为每个视图添加预览 |
| 异步代码中使用非Sendable类型 | 使用带 |
扁平的 | 改用 |