swift_style
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSwift Style
Swift 风格指南
This skill provides comprehensive style guidelines for writing clean, idiomatic, and maintainable Swift code.
本技能为编写整洁、地道且可维护的Swift代码提供全面的风格指南。
Overview
概述
Swift style guidelines cover naming conventions, access control, type selection, and code organization patterns that make your code more readable and professional.
Swift风格指南包含命名规范、访问控制、类型选择以及代码组织模式,可让你的代码更具可读性且更专业。
Available References
可用参考资料
Naming & Conventions
命名与规范
- Variables and Constants - vs
var, naming conventions, immutabilitylet - Functions - Function naming, parameter labels, return types
- 变量与常量 - 与
var的区别、命名规范、不可变性let - 函数 - 函数命名、参数标签、返回类型
Types & Protocols
类型与协议
- Types - Struct vs Class vs Enum, value vs reference semantics
- Protocols - Protocol-Oriented Programming (POP), composition, extensions
- 类型 - Struct、Class与Enum的对比、值类型与引用类型语义
- 协议 - 面向协议编程(POP)、组合、扩展
Code Organization
代码组织
- Access Control - ,
public,private,internal,fileprivateopen
- 访问控制 - 、
public、private、internal、fileprivateopen
Quick Reference
快速参考
Naming Conventions
命名规范
| Category | Case | Example |
|---|---|---|
| Types & Protocols | UpperCamelCase | |
| Variables, Functions | lowerCamelCase | |
| Boolean Properties | | |
| Constants | lowerCamelCase | |
| 类别 | 大小写规则 | 示例 |
|---|---|---|
| 类型与协议 | UpperCamelCase | |
| 变量、函数 | lowerCamelCase | |
| 布尔属性 | 前缀 | |
| 常量 | lowerCamelCase | |
Type Selection Guide
类型选择指南
Need identity or reference semantics?
├── YES → Use Class
└── NO → Need inheritance?
├── YES → Use Class
└── NO → Modeling finite states?
├── YES → Use Enum
└── NO → Use Struct (default)需要标识或引用语义?
├── 是 → 使用Class
└── 否 → 需要继承?
├── 是 → 使用Class
└── 否 → 是否建模有限状态?
├── 是 → 使用Enum
└── 否 → 使用Struct(默认)Access Levels
访问级别
| Modifier | Visibility | Use When |
|---|---|---|
| Enclosing declaration | Strict encapsulation |
| Same file | File-local helpers |
| Same module | Implementation details |
| Everywhere | Public API |
| Everywhere + subclassable | Extensible frameworks |
| 修饰符 | 可见性 | 使用场景 |
|---|---|---|
| 包含它的声明范围内 | 严格封装 |
| 同一文件内 | 文件级辅助工具 |
| 同一模块内 | 实现细节 |
| 所有地方 | 公开API |
| 所有地方 + 可子类化 | 可扩展框架 |
Best Practices
最佳实践
- Default to structs - Use simplest type that expresses intent
- Use by default - Only use
letwhen mutation neededvar - Prefer protocols over inheritance - More flexible composition
- Keep functions focused - Single responsibility
- Use access control - Expose only what's necessary
- Follow naming conventions - Descriptive, Swifty names
- 默认使用struct - 使用最能表达意图的最简单类型
- 默认使用- 仅当需要修改时使用
letvar - 优先使用协议而非继承 - 更灵活的组合方式
- 保持函数聚焦 - 单一职责
- 使用访问控制 - 仅暴露必要内容
- 遵循命名规范 - 描述性的、符合Swift风格的命名
For More Information
更多信息
Each reference file contains detailed information, code examples, and best practices for specific topics. Visit https://swiftzilla.dev for comprehensive Swift documentation.
每个参考文件都包含特定主题的详细信息、代码示例和最佳实践。访问https://swiftzilla.dev获取全面的Swift文档。