swift-format-style
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWrite 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:
- Check for legacy formatting patterns and replace with modern FormatStyle equivalents using .
references/anti-patterns.md - Validate number, percent, and currency formatting using .
references/numeric-styles.md - Validate date and time formatting using .
references/date-styles.md - Validate duration formatting using .
references/duration-styles.md - Validate measurement, list, person name, byte count, and URL formatting using .
references/other-styles.md - 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风格格式化方式。
评审流程:
- 查找遗留格式化模式,参考替换为等效的现代FormatStyle写法。
references/anti-patterns.md - 参考验证数字、百分比、货币格式化逻辑。
references/numeric-styles.md - 参考验证日期时间格式化逻辑。
references/date-styles.md - 参考验证时长格式化逻辑。
references/duration-styles.md - 参考验证度量单位、列表、人名、字节数和URL格式化逻辑。
references/other-styles.md - 参考检查SwiftUI Text视图是否正确集成了FormatStyle。
references/swiftui.md
如果只进行部分工作,仅加载相关的参考文件即可。
Core Instructions
核心说明
- Target iOS 15+ / macOS 12+ minimum for basic FormatStyle. Duration and URL styles require iOS 16+ / macOS 13+.
- Never use legacy subclasses (
Formatter,DateFormatter,NumberFormatter,MeasurementFormatter,DateComponentsFormatter,DateIntervalFormatter,PersonNameComponentsFormatter).ByteCountFormatter - Never use C-style for number formatting. Always use
String(format:)or.formatted()directly.FormatStyle - Never use for formatting on background threads - FormatStyle types are value types and thread-safe.
DispatchQueue - Prefer instance method for simple cases, and explicit
.formatted()types for reusable or complex configurations.FormatStyle - In SwiftUI, use instead of
Text(_:format:).Text("\(value.formatted())") - Use instead of
Decimal/Floatfor currency values.Double - 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 and
Codable, making them safe to store and compare.Hashable
- 基础FormatStyle最低支持iOS 15+ / macOS 12+。时长和URL样式需要iOS 16+ / macOS 13+。
- 严禁使用遗留子类(
Formatter、DateFormatter、NumberFormatter、MeasurementFormatter、DateComponentsFormatter、DateIntervalFormatter、PersonNameComponentsFormatter)。ByteCountFormatter - 严禁使用C风格进行数字格式化。始终使用
String(format:)或直接使用.formatted()。FormatStyle - 严禁使用在后台线程执行格式化操作——FormatStyle类型是值类型且线程安全。
DispatchQueue - 简单场景优先使用实例方法,可复用或复杂配置场景显式使用
.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:
- State the file and relevant line(s).
- Name the anti-pattern being replaced.
- 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:
如果用户要求评审,按文件组织发现的问题。每个问题按以下格式呈现:
- 说明文件和相关行号。
- 指出要替换的反模式名称。
- 展示简短的代码修复前后对比。
跳过无问题的文件。最后附上优先级排序的摘要,列出最需要优先完成的改动。
如果用户要求你编写或修复格式化代码,直接进行改动,而非返回问题报告。
示例输出:
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
摘要
- Legacy formatting (high): C-style String(format:) on line 42 should use Duration.formatted().
- SwiftUI (medium): Text interpolation on line 78 should use the format: parameter directly.
End of example.
- 遗留格式化(高优先级): 第42行的C风格String(format:)应该替换为Duration.formatted()。
- SwiftUI(中优先级): 第78行的字符串插值应该直接使用format:参数。
示例结束。
References
参考文档
- - legacy patterns to replace: String(format:), DateFormatter, NumberFormatter, and other Formatter subclasses.
references/anti-patterns.md - - number, percent, and currency formatting with rounding, precision, sign, notation, scale, and grouping.
references/numeric-styles.md - - date/time compositing, ISO 8601, relative, verbatim, HTTP, interval, and components styles.
references/date-styles.md - - Duration.TimeFormatStyle and Duration.UnitsFormatStyle with patterns, units, width, and fractional seconds.
references/duration-styles.md - - measurement, list, person name, byte count, URL formatting, and custom FormatStyle creation.
references/other-styles.md - - SwiftUI Text integration and best practices.
references/swiftui.md
- - 需要替换的遗留模式:String(format:)、DateFormatter、NumberFormatter和其他Formatter子类。
references/anti-patterns.md - - 包含舍入、精度、符号、计数法、缩放、分组的数字、百分比、货币格式化说明。
references/numeric-styles.md - - 日期/时间组合、ISO 8601、相对时间、原样输出、HTTP、时间间隔、组件样式说明。
references/date-styles.md - - 包含样式、单位、宽度、小数秒的Duration.TimeFormatStyle和Duration.UnitsFormatStyle说明。
references/duration-styles.md - - 度量单位、列表、人名、字节数、URL格式化和自定义FormatStyle创建说明。
references/other-styles.md - - SwiftUI Text集成和最佳实践说明。
references/swiftui.md