swift_style

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Swift 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 -
    var
    vs
    let
    , naming conventions, immutability
  • 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
    ,
    fileprivate
    ,
    open
  • 访问控制 -
    public
    private
    internal
    fileprivate
    open

Quick Reference

快速参考

Naming Conventions

命名规范

CategoryCaseExample
Types & ProtocolsUpperCamelCase
String
,
UIViewController
Variables, FunctionslowerCamelCase
userID
,
fetchData()
Boolean Properties
is
,
has
,
should
isEmpty
,
hasPermission
ConstantslowerCamelCase
maxConnections
类别大小写规则示例
类型与协议UpperCamelCase
String
UIViewController
变量、函数lowerCamelCase
userID
fetchData()
布尔属性前缀
is
has
should
isEmpty
hasPermission
常量lowerCamelCase
maxConnections

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

访问级别

ModifierVisibilityUse When
private
Enclosing declarationStrict encapsulation
fileprivate
Same fileFile-local helpers
internal
(default)
Same moduleImplementation details
public
EverywherePublic API
open
Everywhere + subclassableExtensible frameworks
修饰符可见性使用场景
private
包含它的声明范围内严格封装
fileprivate
同一文件内文件级辅助工具
internal
(默认)
同一模块内实现细节
public
所有地方公开API
open
所有地方 + 可子类化可扩展框架

Best Practices

最佳实践

  1. Default to structs - Use simplest type that expresses intent
  2. Use
    let
    by default
    - Only use
    var
    when mutation needed
  3. Prefer protocols over inheritance - More flexible composition
  4. Keep functions focused - Single responsibility
  5. Use access control - Expose only what's necessary
  6. Follow naming conventions - Descriptive, Swifty names
  1. 默认使用struct - 使用最能表达意图的最简单类型
  2. 默认使用
    let
    - 仅当需要修改时使用
    var
  3. 优先使用协议而非继承 - 更灵活的组合方式
  4. 保持函数聚焦 - 单一职责
  5. 使用访问控制 - 仅暴露必要内容
  6. 遵循命名规范 - 描述性的、符合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文档。