swift-format-style

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
Write and review Swift code that formats values for display, ensuring modern FormatStyle APIs are used instead of legacy Formatter subclasses or C-style formatting.
Review process:
  1. Check for legacy formatting patterns and replace with modern FormatStyle equivalents using
    references/anti-patterns.md
    .
  2. Validate number, percent, and currency formatting using
    references/numeric-styles.md
    .
  3. Validate date and time formatting using
    references/date-styles.md
    .
  4. Validate duration formatting using
    references/duration-styles.md
    .
  5. Validate measurement, list, person name, byte count, and URL formatting using
    references/other-styles.md
    .
  6. Check SwiftUI Text views for proper FormatStyle integration using
    references/swiftui.md
    .
If doing partial work, load only the relevant reference files.
编写和评审用于格式化展示值的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。
如果只进行部分工作,仅加载相关的参考文件即可。

Core Instructions

核心说明

  • Target iOS 15+ / macOS 12+ minimum for basic FormatStyle. Duration and URL styles require iOS 16+ / macOS 13+.
  • Never use legacy
    Formatter
    subclasses (
    DateFormatter
    ,
    NumberFormatter
    ,
    MeasurementFormatter
    ,
    DateComponentsFormatter
    ,
    DateIntervalFormatter
    ,
    PersonNameComponentsFormatter
    ,
    ByteCountFormatter
    ).
  • Never use C-style
    String(format:)
    for number formatting. Always use
    .formatted()
    or
    FormatStyle
    directly.
  • Never use
    DispatchQueue
    for formatting on background threads - FormatStyle types are value types and thread-safe.
  • Prefer
    .formatted()
    instance method for simple cases, and explicit
    FormatStyle
    types for reusable or complex configurations.
  • In SwiftUI, use
    Text(_:format:)
    instead of
    Text("\(value.formatted())")
    .
  • Use
    Decimal
    instead of
    Float
    /
    Double
    for currency values.
  • FormatStyle types are locale-aware by default. Only set locale explicitly when you need a specific locale different from the user's current locale.
  • FormatStyle types conform to
    Codable
    and
    Hashable
    , making them safe to store and compare.
  • 基础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
    协议,可安全存储和比较。

Output Format

输出格式

If the user asks for a review, organize findings by file. For each issue:
  1. State the file and relevant line(s).
  2. Name the anti-pattern being replaced.
  3. Show a brief before/after code fix.
Skip files with no issues. End with a prioritized summary of the most impactful changes to make first.
If the user asks you to write or fix formatting code, make the changes directly instead of returning a findings report.
Example output:
如果用户要求评审,按文件组织发现的问题。每个问题按以下格式呈现:
  1. 说明文件和相关行号。
  2. 指出要替换的反模式名称。
  3. 展示简短的代码修复前后对比。
跳过无问题的文件。最后附上优先级排序的摘要,列出最需要优先完成的改动。
如果用户要求你编写或修复格式化代码,直接进行改动,而非返回问题报告。
示例输出:

RecordingView.swift

RecordingView.swift

Line 42: Use Duration.formatted() instead of String(format:) for time display.
swift
// 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))
Line 78: Use Text(_:format:) instead of string interpolation.
swift
// Before
Text("\(fileSize.formatted(.byteCount(style: .file)))")

// After
Text(fileSize, format: .byteCount(style: .file))
Line 42: Use Duration.formatted() instead of String(format:) for time display.
swift
// 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))
Line 78: Use Text(_:format:) instead of string interpolation.
swift
// Before
Text("\(fileSize.formatted(.byteCount(style: .file)))")

// After
Text(fileSize, format: .byteCount(style: .file))

Summary

摘要

  1. Legacy formatting (high): C-style String(format:) on line 42 should use Duration.formatted().
  2. SwiftUI (medium): Text interpolation on line 78 should use the format: parameter directly.
End of example.
  1. 遗留格式化(高优先级): 第42行的C风格String(format:)应该替换为Duration.formatted()。
  2. SwiftUI(中优先级): 第78行的字符串插值应该直接使用format:参数。
示例结束。

References

参考文档

  • references/anti-patterns.md
    - legacy patterns to replace: String(format:), DateFormatter, NumberFormatter, and other Formatter subclasses.
  • references/numeric-styles.md
    - number, percent, and currency formatting with rounding, precision, sign, notation, scale, and grouping.
  • references/date-styles.md
    - date/time compositing, ISO 8601, relative, verbatim, HTTP, interval, and components styles.
  • references/duration-styles.md
    - Duration.TimeFormatStyle and Duration.UnitsFormatStyle with patterns, units, width, and fractional seconds.
  • references/other-styles.md
    - measurement, list, person name, byte count, URL formatting, and custom FormatStyle creation.
  • references/swiftui.md
    - SwiftUI Text integration and best practices.
  • 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集成和最佳实践说明。