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.