All skills
dpearson2699 avatar

/swift-formatstyle

@cf3fe87

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.

Use this Skill: https://skilld.dev/gh/dpearson2699/swift-ios-skills/swift-formatstyle

This session only. Nothing lands on disk.

referencesformatstyle-recipes.md

≈1.1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

FormatStyle Recipes

Load this reference when concrete formatting modifiers or less common built-in styles are needed.

Contents

Numbers and Decimals

1234.5.formatted(.number.precision(.fractionLength(0...2)))
1234.5.formatted(.number.precision(.significantDigits(3)))
1234.formatted(.number.rounded(rule: .down, increment: 100))
1_200_000.formatted(.number.notation(.compactName))
42.formatted(.number.notation(.scientific))
(-42).formatted(.number.sign(strategy: .always()))

let amount = Decimal(string: "12345.67")!
amount.formatted(.number)
amount.formatted(.number.grouping(.never))
let parsed = try? Decimal("3.500,63", format: .number.locale(.init(identifier: "de_DE")))

Docs: IntegerFormatStyle · Decimal.FormatStyle

Currency and Percent

29.99.formatted(.currency(code: "USD"))
Decimal(string: "12345.67")!.formatted(.currency(code: "EUR"))
0.8567.formatted(.percent.precision(.fractionLength(1)))

Confirm the percent input contract: floating-point .percent commonly treats 0.85 as 85%, while integer 42.formatted(.percent) displays 42%.

Dates

let now = Date.now
now.formatted(.dateTime.weekday(.wide).month(.wide).day())
now.formatted(date: .long, time: .shortened)
now.formatted(.iso8601)

let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: now)!
yesterday.formatted(.relative(presentation: .named))
(start..<end).formatted(.interval.month().day().hour().minute())
(start..<end).formatted(.components(style: .wide, fields: [.day, .hour]))

Use Date.AnchoredRelativeFormatStyle on iOS 18+ when the anchor must be fixed rather than Date.now. Preview relative output as standalone text; embedding it can produce ungrammatical localized sentences.

Docs: Date.FormatStyle · Date.RelativeFormatStyle · Date.IntervalFormatStyle

Durations

let duration = Duration.seconds(3661)

duration.formatted(.time(pattern: .hourMinuteSecond))
Duration.seconds(3.75).formatted(
    .time(pattern: .minuteSecond(padMinuteToLength: 2, fractionalSecondsLength: 2))
)
duration.formatted(
    .units(allowed: [.hours, .minutes, .seconds], width: .abbreviated,
           maximumUnitCount: 2)
)

Use .time(pattern:) for separator-based clock output and .units(allowed:width:) for labeled prose. Both require iOS 16+.

Measurements and Names

let temperature = Measurement(value: 72, unit: UnitTemperature.fahrenheit)
temperature.formatted(.measurement(width: .abbreviated))

let distance = Measurement(value: 5, unit: UnitLength.kilometers)
distance.formatted(.measurement(width: .abbreviated, usage: .road))

var name = PersonNameComponents()
name.givenName = "Thomas"
name.familyName = "Clark"
name.nickname = "Tom"
name.formatted(.name(style: .short))

Do not manually concatenate person names. Style resolution accounts for script, user preferences, locale, and requested style.

Lists and Byte Counts

["Alice", "Bob", "Charlie"].formatted(.list(type: .and))
[1, 2, 3].formatted(.list(memberStyle: .number, type: .or))
Int64(1_048_576).formatted(.byteCount(style: .memory))
Int64(1_048_576).formatted(.byteCount(style: .binary))

URLs

URL.FormatStyle requires iOS 16+. Scheme, host, and path are the normal display surface; port, query, and fragment are opt-in.

let url = URL(string: "https://www.example.com:8080/path?q=1#section")!
url.formatted(.url.scheme(.never).host().path())
url.formatted(.url.scheme(.never).host().path().port(.always).query(.always))
url.formatted(.url.scheme(.never).host().path().fragment(.always))

Treat this as display formatting, not URL sanitization or a security boundary.

Source: SKILL.md on GitHub

No alerts17d3 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    The swift-formatstyle skill is a safe, instructional resource providing guidance and code recipes for using Swift's Foundation FormatStyle APIs. It contains no malicious patterns, external code execution, or data exfiltration risks.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

Signed by skilld at cf3fe87. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Steadyupdated 3 months ago

README badge

README badge for dpearson2699/swift-ios-skills/swift-formatstyle