用途:整理现代 Swift 语言模式与惯用法,适用于非并发、非 SwiftUI 的代码,目标版本为 Swift 6.3。并发(actor、async/await、Sendable)见 swift-concurrency,SwiftUI 视图与状态管理见 swiftui-patterns。
主要能力:if/switch 表达式(Swift 5.9+,各分支须同类型且为单表达式);typed throws(Swift 6+,throws(SomeError),throws(Never) 表示语法上抛出但实际不抛);@resultBuilder 自定义 DSL;@propertyWrapper 封装存储与访问;some 与 any 的选择;guard 前置校验;Never 类型;RegexBuilder;Codable 最佳实践(CodingKeys、decodeIfPresent、嵌套容器);现代集合 API(count(where:)、contains(where:)、compactMap、flatMap、Dictionary(grouping:by:)、reduce(into:));FormatStyle 格式化与解析;字符串插值;常见错误与评审清单。
使用方式:按主题查阅对应模式并套用到代码中;复杂正则用构建器,简单模式可用字面量;格式化优先用 .formatted() 而非 DateFormatter/NumberFormatter。
适用场景:日常 Swift 业务代码、模型编解码、数据格式化与集合处理。
限制:不涉及并发与 SwiftUI;扩展模式见 references/swift-patterns-extended.md,属性与 C 互操作(@c、@specialized、@inline(always)、@export、ModuleName::symbol)见 references/swift-attributes-interop.md;具体安装或集成方式暂未提供。