axiom-swift-modern
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseModern Swift Idioms
现代Swift惯用写法
Purpose
用途
Claude frequently generates outdated Swift patterns from its training data. This skill corrects the most common ones — patterns that compile fine but use legacy APIs when modern equivalents are clearer, more efficient, or more correct.
Philosophy: "Don't repeat what LLMs already know — focus on edge cases, surprises, soft deprecations." (Paul Hudson)
Claude的训练数据中经常会生成过时的Swift代码模式。本指南用于修正最常见的这类问题——这些模式虽然可以正常编译,但使用了遗留API,而对应的现代等效写法更清晰、更高效或者更准确。
理念:「不要重复LLM已经知道的内容——聚焦边界场景、意外问题、软废弃特性。」(Paul Hudson)
Modern API Replacements
现代API替换
| Old Pattern | Modern Swift | Since | Why |
|---|---|---|---|
| | 5.6 | Clearer intent |
| | 5.0 | Single pass, no intermediate allocation |
| | 5.7 | Swift native, no Foundation bridge |
| | 5.5 | Implicit bridging; exceptions: optionals, inout, ObjC-bridged APIs |
| | 5.7 | Type-safe Duration API |
| | 5.5 | No instance management, localizable by default |
| | 5.5 | Type-safe, localized |
| | 5.0 | Handles diacritics, ligatures, width variants |
| | 5.5 | Respects locale name ordering |
| | 5.6 | Modern parsing (throws); use "y" not "yyyy" for display |
| | 5.0 | Required for correct text search/filtering |
| 旧写法 | 现代Swift写法 | 起始版本 | 原因 |
|---|---|---|---|
| | 5.6 | 意图更清晰 |
| | 5.0 | 单次遍历,无中间内存分配 |
| | 5.7 | Swift原生实现,无需Foundation桥接 |
| | 5.5 | 隐式桥接;例外情况:可选值、inout参数、ObjC桥接API |
| | 5.7 | 类型安全的Duration API |
| | 5.5 | 无需实例管理,默认支持本地化 |
| | 5.5 | 类型安全,支持本地化 |
| | 5.0 | 支持变音符号、连字、宽度变体处理 |
| 配合 | 5.5 | 遵循地区姓名排序规则 |
| | 5.6 | 现代解析能力(支持抛出异常);展示场景请使用"y"而非"yyyy" |
对用户输入调用 | | 5.0 | 正确实现文本搜索/过滤的必要写法 |
Modern Syntax
现代语法
| Old Pattern | Modern Swift | Since |
|---|---|---|
| | 5.7 |
Explicit | Omit | 5.9 |
| | 5.5 |
| Often not needed — SwiftUI re-exports most UIKit/AppKit types. Retain for UIKit-only APIs ( | 5.5 |
| 旧写法 | 现代Swift写法 | 起始版本 |
|---|---|---|
| | 5.7 |
单表达式中显式写 | 省略 | 5.9 |
修饰符中写 | | 5.5 |
同时 | 通常不需要——SwiftUI会重新导出大部分UIKit/AppKit类型。仅在使用UIKit专属API(如 | 5.5 |
Foundation Modernization
Foundation现代化
| Old Pattern | Modern Foundation | Since |
|---|---|---|
| | 5.7 |
| | 5.7 |
| Conform to | — |
| | — |
| 旧写法 | 现代Foundation写法 | 起始版本 |
|---|---|---|
| | 5.7 |
| | 5.7 |
重复使用 | 遵循 | — |
展示用日期格式中写 | | — |
SwiftUI Convenience APIs Claude Misses
Claude遗漏的SwiftUI便捷API
- (iOS 17+) automatically includes the search term — no need to compose a custom string
ContentUnavailableView.search(text: searchText) - in Forms (iOS 16+) provides consistent label alignment without manual HStack layout
LabeledContent - must attach to triggering UI — Liquid Glass morphing animations depend on the source element
confirmationDialog()
- (iOS 17+)自动包含搜索词——无需手动拼接自定义字符串
ContentUnavailableView.search(text: searchText) - 表单中的(iOS 16+)无需手动写HStack布局即可实现一致的标签对齐效果
LabeledContent - 必须挂载到触发的UI元素上——毛玻璃变形动画依赖源元素定位
confirmationDialog()
Common Claude Hallucinations
Claude常见幻觉
These patterns appear frequently in Claude-generated code:
- Creates instances inline — Use
DateFormatteror.formatted()instead. If a formatter must exist, make itFormatStyle.static let - Uses — Use
DispatchQueue.main.asyncor@MainActor. Never GCD. (SeeMainActor.runfor full guidance.)axiom-swift-concurrency - Uses for SwiftUI parameters —
CGFloatworks everywhere since Swift 5.5 implicit bridging.Double - Generates — Use
guard let x = x elseshorthand.guard let x else - Returns explicitly in single-expression computed properties — Omit .
return
这些模式在Claude生成的代码中频繁出现:
- 内联创建实例——请改用
DateFormatter或.formatted()。如果必须使用formatter,请声明为FormatStyle。static let - 使用——请使用
DispatchQueue.main.async或@MainActor,永远不要使用GCD。(完整指南请参考MainActor.run)axiom-swift-concurrency - SwiftUI参数使用——自Swift 5.5隐式桥接支持以来,
CGFloat可在所有场景下正常使用。Double - 生成写法——请使用
guard let x = x else简写。guard let x else - 单表达式计算属性中显式写return——省略即可。
return
Resources
资源
Skills: axiom-swift-performance, axiom-swift-concurrency, axiom-swiftui-architecture
相关技能:axiom-swift-performance, axiom-swift-concurrency, axiom-swiftui-architecture