mcpField
S
swift-format-style

swift-format-style

github.com/n0an开发15458 次安装8 周前更新
代码仓库

关于 swift-format-style

面向 Swift 开发者的代码格式化技能,聚焦用现代 FormatStyle API 替代旧式 Formatter 和 C 风格格式化,并提供按类型检查的参考资料。

主要功能

  1. 01识别并替换旧式格式化写法
  2. 02验证数字、百分比与货币格式
  3. 03检查日期、时间和时长格式
  4. 04覆盖度量、列表及 URL 等格式
  5. 05检查 SwiftUI Text 格式集成

适用场景

  1. 01审查 Swift 代码中的格式化问题
  2. 02迁移 DateFormatter 等旧式 API
  3. 03实现本地化数字或日期显示
  4. 04修正 SwiftUI 文本的格式用法

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/n0an/swift-formatstyle-agent-skill --skill swift-format-style

安装后在 Claude Code 中输入/swift-format-style确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

agents/
openai.yaml
references/
anti-patterns.md
date-styles.md
duration-styles.md
numeric-styles.md
other-styles.md
swiftui.md

详细介绍

编写和审查 Swift 代码,以格式化供显示的值;确保使用现代 FormatStyle API,而不是旧版 Formatter 子类或 C 风格格式化。

审查流程:

  1. 检查旧版格式化模式,并参照 references/anti-patterns.md 替换为现代 FormatStyle 等效写法。
  2. 参照 references/numeric-styles.md 验证数字、百分比和货币格式化。
  3. 参照 references/date-styles.md 验证日期和时间格式化。
  4. 参照 references/duration-styles.md 验证时长格式化。
  5. 参照 references/other-styles.md 验证测量值、列表、人名、字节数和 URL 格式化。
  6. 参照 references/swiftui.md 检查 SwiftUI Text 视图是否正确集成 FormatStyle。

如果只处理部分内容,只加载相关的参考文件。

核心说明

  • 基本 FormatStyle 的最低目标版本为 iOS 15+ / macOS 12+。时长和 URL 样式要求 iOS 16+ / macOS 13+。
  • 绝不使用旧版 Formatter 子类(DateFormatter、NumberFormatter、MeasurementFormatter、DateComponentsFormatter、DateIntervalFormatter、PersonNameComponentsFormatter、ByteCountFormatter)。
  • 绝不使用 C 风格的 String(format:) 格式化数字。始终使用 .formatted() 或直接使用 FormatStyle。
  • 绝不使用 DispatchQueue 在后台线程上进行格式化——FormatStyle 类型是值类型,且线程安全。
  • 简单情况优先使用 .formatted() 实例方法;对于可复用或复杂的配置,使用明确的 FormatStyle 类型。
  • 在 SwiftUI 中,使用 Text(_:format:),而不是 Text("\(value.formatted())")。
  • 货币值使用 Decimal,而不是 Float / Double。
  • FormatStyle 类型默认会根据区域设置进行格式化。只有在需要使用不同于用户当前区域设置的特定区域设置时,才显式指定 locale。
  • FormatStyle 类型遵循 Codable 和 Hashable,可安全存储和比较。

输出格式

如果用户要求审查,请按文件整理发现的问题。对于每个问题:

  1. 指出文件和相关行号。
  2. 说明要替换的反模式。
  3. 展示简短的修改前 / 修改后代码。

跳过没有问题的文件。最后按优先级总结最重要的修改项。

输出示例:

RecordingView.swift

第 42 行:使用 Duration.formatted() 代替 String(format:) 显示时间。

// Before
let minutes = Int(duration) / 60
let seconds = Int(duration) % 60
return String(format: "%02d:%02d", minutes, seconds)

// After
Duration.seconds(duration).formatted(.time(pattern: .minuteSecond))

第 78 行:使用 Text(_:format:) 代替字符串插值。

// Before
Text("\(fileSize.formatted(.byteCount(style: .file)))")

// After
Text(fileSize, format: .byteCount(style: .file))
总结
  1. 旧版格式化(高): 第 42 行的 C 风格 String(format:) 应改用 Duration.formatted()。
  2. SwiftUI(中): 第 78 行的 Text 插值应直接使用 format: 参数。

示例结束。

参考资料

  • references/anti-patterns.md - 要替换的旧版模式:String(format:)、DateFormatter、NumberFormatter 和其他 Formatter 子类。
  • references/numeric-styles.md - 数字、百分比和货币格式化,包括舍入、精度、符号、记数法、缩放和分组。
  • references/date-styles.md - 日期 / 时间组合、ISO 8601、相对、逐字、HTTP、区间和组件样式。
  • references/duration-styles.md - Duration.TimeFormatStyle 和 Duration.UnitsFormatStyle,包括模式、单位、宽度和小数秒。
  • references/other-styles.md - 测量值、列表、人名、字节数、URL 格式化和自定义 FormatStyle 的创建。
  • references/swiftui.md - SwiftUI Text 集成和最佳实践。

评论

支持 Markdown