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:
- 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.
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.
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
Line 42: Use Duration.formatted() instead of String(format:) for time display.
// 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.
// 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.
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.
1---2name: swift-format-style3description: Writes and reviews Swift FormatStyle code, replacing legacy Formatter subclasses and C-style String(format:) with modern .formatted() APIs. Use when formatting numbers, dates, durations, measurements, lists, names, byte counts, or URLs.4license: MIT5---6
7Write and review Swift code that formats values for display, ensuring modern FormatStyle APIs are used instead of legacy Formatter subclasses or C-style formatting.
8
9Review process:
10
111. Check for legacy formatting patterns and replace with modern FormatStyle equivalents using `references/anti-patterns.md`.
121. Validate number, percent, and currency formatting using `references/numeric-styles.md`.
131. Validate date and time formatting using `references/date-styles.md`.
141. Validate duration formatting using `references/duration-styles.md`.
151. Validate measurement, list, person name, byte count, and URL formatting using `references/other-styles.md`.
161. Check SwiftUI Text views for proper FormatStyle integration using `references/swiftui.md`.
17
18If doing partial work, load only the relevant reference files.
19
20
21## Core Instructions
22
23- Target iOS 15+ / macOS 12+ minimum for basic FormatStyle. Duration and URL styles require iOS 16+ / macOS 13+.
24- **Never** use legacy `Formatter` subclasses (`DateFormatter`, `NumberFormatter`, `MeasurementFormatter`, `DateComponentsFormatter`, `DateIntervalFormatter`, `PersonNameComponentsFormatter`, `ByteCountFormatter`).
25- **Never** use C-style `String(format:)` for number formatting. Always use `.formatted()` or `FormatStyle` directly.
26- **Never** use `DispatchQueue` for formatting on background threads - FormatStyle types are value types and thread-safe.
27- Prefer `.formatted()` instance method for simple cases, and explicit `FormatStyle` types for reusable or complex configurations.
28- In SwiftUI, use `Text(_:format:)` instead of `Text("\(value.formatted())")`.
29- Use `Decimal` instead of `Float`/`Double` for currency values.
30- FormatStyle types are locale-aware by default. Only set locale explicitly when you need a specific locale different from the user's current locale.
31- FormatStyle types conform to `Codable` and `Hashable`, making them safe to store and compare.
32
33
34## Output Format
35
36If the user asks for a review, organize findings by file. For each issue:
37
381. State the file and relevant line(s).
392. Name the anti-pattern being replaced.
403. Show a brief before/after code fix.
41
42Skip files with no issues. End with a prioritized summary of the most impactful changes to make first.
43
44If the user asks you to write or fix formatting code, make the changes directly instead of returning a findings report.
45
46Example output:
47
48### RecordingView.swift
49
50**Line 42: Use Duration.formatted() instead of String(format:) for time display.**
51
52```swift
53// Before
54let minutes = Int(duration) / 60
55let seconds = Int(duration) % 60
56return String(format: "%02d:%02d", minutes, seconds)
57
58// After
59Duration.seconds(duration).formatted(.time(pattern: .minuteSecond))
60```
61
62**Line 78: Use Text(_:format:) instead of string interpolation.**
63
64```swift
65// Before
66Text("\(fileSize.formatted(.byteCount(style: .file)))")
67
68// After
69Text(fileSize, format: .byteCount(style: .file))
70```
71
72### Summary
73
741. **Legacy formatting (high):** C-style String(format:) on line 42 should use Duration.formatted().
752. **SwiftUI (medium):** Text interpolation on line 78 should use the format: parameter directly.
76
77End of example.
78
79
80## References
81
82- `references/anti-patterns.md` - legacy patterns to replace: String(format:), DateFormatter, NumberFormatter, and other Formatter subclasses.
83- `references/numeric-styles.md` - number, percent, and currency formatting with rounding, precision, sign, notation, scale, and grouping.
84- `references/date-styles.md` - date/time compositing, ISO 8601, relative, verbatim, HTTP, interval, and components styles.
85- `references/duration-styles.md` - Duration.TimeFormatStyle and Duration.UnitsFormatStyle with patterns, units, width, and fractional seconds.
86- `references/other-styles.md` - measurement, list, person name, byte count, URL formatting, and custom FormatStyle creation.
87- `references/swiftui.md` - SwiftUI Text integration and best practices.