Skip to content

Foundation Formatter API Basics (Swift)

A SwiftUI guide for coding agents. Also covers formatted function, format date without dateformatter, number currency percent format, formatstyle api, locale friendly formatting ios, relative date string.

The formatted() family replaces class-based formatters such as DateFormatter with value-oriented format styles. Foundation caches the machinery itself, so you stop managing formatter instances.

Why the Old Formatters Hurt

Creating a DateFormatter is expensive, so the old API forces you to cache instances and never build one per collection item. It is also a mutable class: one accidental configuration change affects every place that shares the instance.

Formatted in Place

Most Foundation types answer formatted(), with a default locale-aware output and style builders for everything else. Type .formatted() on a value in Xcode to discover what it supports.

Date.now.formatted()
["Ada", "Grace"].formatted(.list(type: .and))

Date Styles

The date styles mix and match, from relative phrasing to field-by-field composition.

Date.now.formatted(.relative(presentation: .named))
Date.now.formatted(date: .abbreviated, time: .omitted)
Date.now.formatted(.dateTime.day(.twoDigits).month(.abbreviated).year(.twoDigits))
Date.now.formatted(.iso8601)

Numbers and Measurements

Number styles cover precision, percent, and currency. Measurement formats its unit too.

1_000_000_000.formatted()
1.001.formatted(.number.precision(.fractionLength(1)))
0.1.formatted(.percent)
0.99.formatted(.currency(code: "USD"))

Measurement<UnitMass>(value: 1, unit: .kilograms)
    .formatted(.measurement(width: .abbreviated))

The styles are value types, so a customized style cannot leak configuration into other call sites the way a shared formatter instance can.

Use this guidance in your coding agent

Install the Better Design MCP once. Your agent then loads this page with one call.

get-swiftui-guide({ topic: "swiftui-mastering-swift-foundation-formatter-api-basics" })
claude mcp add --scope user better-design --transport http https://better-design.com/api/mcp --header "Authorization: Bearer <YOUR_API_KEY>"
Browse related design systems