PluginBench
Skill
Pass
Audit score 90

swift-formatstyle

dpearson2699/swift-ios-skills

Type-safe locale-aware formatting and parsing for numbers, dates, currencies, measurements, and more with Foundation FormatStyle.

What is swift-formatstyle?

Swift FormatStyle provides modern, type-safe APIs for formatting and parsing user-facing values across locales. Use it to replace legacy Formatter subclasses, design reusable format conventions, integrate with SwiftUI Text(_:format:), and ensure correct availability and locale handling.

  • Format and parse numbers, decimals, currencies, and percentages with locale awareness
  • Handle dates, durations, measurements, names, lists, byte counts, and URLs with built-in styles
  • Create custom FormatStyle implementations for domain-specific conventions
  • Round-trip values through ParseableFormatStyle for user input and import workflows
  • Integrate seamlessly with SwiftUI Text(_:format:) for reactive formatted display

How to install swift-formatstyle

npx skills add https://github.com/dpearson2699/swift-ios-skills --skill swift-formatstyle
Prerequisites
  • iOS 15+ for FormatStyle (iOS 16+ for Duration and URL.FormatStyle, iOS 18+ for AnchoredRelativeFormatStyle)
  • Foundation framework
Claude Code
Cursor
Windsurf
Cline

How to use swift-formatstyle

  1. 1.Identify the semantic type of the value and whether it must support round-trip parsing
  2. 2.Select the narrowest built-in FormatStyle (e.g., .currency(code:), .dateTime, .measurement)
  3. 3.Omit .locale() to inherit the user's current locale; use a fixed locale only for explicit protocols or test fixtures
  4. 4.For SwiftUI, use Text(_:format:) with the chosen style rather than manual formatting
  5. 5.Test the exact rendered UI across representative locales and verify parse round trips work correctly

Use cases

Good for
  • Replace legacy NumberFormatter, DateFormatter, and DateComponentsFormatter in iOS 15+ code
  • Display currency amounts with correct symbols and separators for the user's locale
  • Format relative dates and durations for standalone or embedded UI text
  • Parse user-edited formatted values back to their original types with matching styles
  • Test formatted output across representative locales (en_US, de_DE, ar_SA, ja_JP)
Who it's for
  • iOS developers modernizing formatting code
  • Teams building multi-locale applications
  • Developers designing reusable format APIs and domain conventions
  • QA and localization reviewers validating user-facing formatted output

swift-formatstyle FAQ

When should I use FormatStyle instead of legacy formatters?

Prefer FormatStyle for all new iOS 15+ code. It is type-safe, composable, and integrates directly with SwiftUI. Legacy formatters (NumberFormatter, DateFormatter) should only be used for backward compatibility with older iOS versions.

How do I format currency correctly across locales?

Always pass an ISO 4217 currency code (e.g., .currency(code: "USD")) and never hardcode a symbol. Omit .locale() to inherit the user's locale, unless the input contract explicitly requires a fixed locale. Test the output across representative locales.

What is the difference between FormatStyle and ParseableFormatStyle?

FormatStyle converts a value to a formatted string for display. ParseableFormatStyle does both: it formats for display and parses the same format back to the original value. Use ParseableFormatStyle when users must edit or import formatted values.

Should I use relative dates in sentences or standalone?

Relative date output (e.g., "2 hours ago") should usually stand alone as its own text element. If you must embed it in a sentence, localize the entire sentence, not just the relative date component.

How do I test FormatStyle across locales?

Create a representative-locale matrix (en_US, de_DE, ar_SA, ja_JP) and render the exact UI with each locale. Verify separators, numbering systems, calendars, currency placement, text direction, and parse round trips all work correctly before shipping.

Full instructions (SKILL.md)

Source of truth, from dpearson2699/swift-ios-skills.


name: swift-formatstyle description: "Formats and parses locale-aware numbers, decimals, currencies, percentages, dates, durations, measurements, names, lists, byte counts, and URLs with Foundation FormatStyle and ParseableFormatStyle. Use when replacing legacy Formatter subclasses, designing reusable format APIs, using SwiftUI Text(_:format:), correcting availability, or testing user-facing formatted output across locales."

Swift FormatStyle

Use Foundation's type-safe FormatStyle APIs for user-facing display and ParseableFormatStyle when the same convention must accept input. Route String Catalogs, plurals, localized copy, bundles, and RTL layout to ios-localization; formatting still requires locale review even when no text is translated.

Contents

Workflow

  1. Identify the value's semantic type and whether output is display-only or must round-trip through parsing.
  2. Select the narrowest built-in style and keep the user's current locale unless a wire format explicitly requires another locale.
  3. Render the exact UI with representative locales such as en_US, de_DE, ar_SA, and ja_JP.
  4. Check separators, numbering systems, calendars, currency and unit conventions, text direction, and layout.
  5. If output or parsing fails, fix the style or surrounding copy, restore the input fixture, and rerun the same locale matrix.

Load FormatStyle Recipes when implementation needs concrete modifiers, date intervals, relative dates, duration patterns, measurements, names, lists, byte counts, or URL component controls.

Quick Reference

ValueDefault styleImportant constraint
Int, Double.numberConfigure precision, rounding, grouping, notation, or sign only as required.
Decimal.number, .percent, .currency(code:)Prefer for exact decimal display and parsing.
Currency.currency(code:)Pass an ISO 4217 code; never hardcode a symbol.
Percent.percentConfirm whether the input is a fraction (0.85) or whole percent (85).
Date.dateTime, .relative, .intervalRelative output should usually stand alone, not be embedded in a sentence.
Duration.time(pattern:), .units(allowed:width:)Requires iOS 16+; choose compact clock output versus labeled units.
Measurement.measurement(width:usage:)usage: controls locale-aware conversion policy.
PersonNameComponents.name(style:)Do not manually concatenate name parts.
Collections.list(type:width:)Let locale rules choose separators and conjunctions.
Byte count.byteCount(style:)Choose file, memory, decimal, or binary semantics deliberately.
URL.urlRequires iOS 16+; query, port, and fragment are opt-in display components.

Selection and Availability

  • Prefer FormatStyle over legacy NumberFormatter, DateFormatter, DateComponentsFormatter, and manual interpolation for new iOS 15+ code.
  • Duration format styles and URL.FormatStyle require iOS 16+.
  • Date.AnchoredRelativeFormatStyle requires iOS 18+ and formats relative to a fixed anchor rather than the current moment.
  • Omit .locale(...) for normal UI so the style inherits the user's locale. Use a fixed locale only for an explicit protocol or test fixture.
  • Treat relative date output as standalone text unless the entire sentence is localized around it.

Docs: FormatStyle

Parsing

Use the matching parseable style when users edit or import formatted values:

let style = Decimal.FormatStyle.Currency(code: "USD")
    .locale(Locale(identifier: "en_US"))

let value = try Decimal("$3,500.63", format: style)
let display = value.formatted(style)

The fixed locale above is appropriate only because the input contract is explicitly en_US. For normal UI input, use the user's locale and test round trips with representative decimal separators, currency placement, calendars, and numbering systems.

SwiftUI Integration

Prefer Text(_:format:) so SwiftUI owns the formatted value:

Text(price, format: .currency(code: currencyCode))
Text(date, format: .dateTime.month().day().year())
Text(duration, format: .units(allowed: [.minutes, .seconds]))

For every user-facing formatted Text, preview or test the exact screen across the locale matrix. Use Text(.now, style: .timer), Text(.now, style: .relative), or Text(timerInterval:) for live-updating time displays rather than manually scheduling string refreshes.

Custom FormatStyle

Create a custom style only when built-in composition cannot express the domain convention. FormatStyle refines Codable and Hashable; keep styles as reusable values and add ParseableFormatStyle only when input must round-trip.

struct AbbreviatedCountStyle: FormatStyle {
    func format(_ value: Int) -> String {
        switch value {
        case ..<1_000: "\(value)"
        case 1_000..<1_000_000: String(format: "%.1fK", Double(value) / 1_000)
        default: String(format: "%.1fM", Double(value) / 1_000_000)
        }
    }
}

extension FormatStyle where Self == AbbreviatedCountStyle {
    static var abbreviatedCount: Self { .init() }
}

Custom styles still need locale and accessibility review; a compact English suffix may not be suitable for every locale.

Common Mistakes

MistakeFix
Manual formatting or legacy formatter allocation in view codeUse the matching FormatStyle and Text(_:format:).
Hardcoded currency symbol or localePass an ISO currency code and inherit the user locale unless the contract says otherwise.
Binary floating-point for exact decimal inputUse Decimal.FormatStyle and its matching parse strategy.
Assuming URL.formatted() preserves every componentOpt in to .port(.always), .query(.always), or .fragment(.always) only when those components should display.
Relative date output embedded in a larger sentenceKeep it standalone or localize the complete sentence.
Clock-style duration used for proseUse .units(allowed:width:) for labeled output.
Measurement conversion left implicitSelect a usage: appropriate to the UI.
One-locale spot checkRun the same exact-screen fixture across representative locales and fix/rerun failures.

Review Checklist

  • Semantic value type and display-versus-parse requirement identified
  • Built-in FormatStyle used where it can express the convention
  • Availability gated beside iOS 16+ and iOS 18+ APIs
  • Currency uses an ISO 4217 code; exact decimal values use Decimal
  • User locale inherited unless a protocol explicitly fixes it
  • Relative date text stands alone or the full sentence is localized
  • URL components and measurement usage: are deliberate
  • SwiftUI uses Text(_:format:) for formatted values
  • Exact rendered UI and parse round trips pass the representative-locale matrix

References