Swift has more formatters than you know!


You’re more of a video kind of person? I’ve got you covered! Here’s a video with the same content than this article 🍿


If you have some experience with iOS, I’m pretty sure you’ve already used a DateFormatter or a NumberFormatter.

And you’ve probably noticed how much time these formatters save you versus if you’d had to write all of that formatting logic yourself!

But did you know that Swift has more formatters than just these two?

Let’s have a look at 5 other formatters that come built-in with iOS!

#01 – DateComponentsFormatter

As its name does suggest, DateComponentsFormatter is a close relative of DateFormatter.

But whereas DateFormatter is great to format a given point in time, DateComponentsFormatter allows you to format quantities of time.

A typical use case would be to format the amount of time remaining before the end of a lengthy operation, like when downloading a large file.

#02 – DateIntervalFormatter

Another close relative of DateFormatter is DateIntervalFormatter, which allows you to format user-facing pairs of dates.

It’s a less common use case for sure, but any app that lets users handle calendar events might find it pretty useful!

#03 – MeasurementFormatter

Dealing with physical units is a nightmare and it’s definitely not a topic on which any iOS developer wants to spend their time and energy on.

So if your app displays any kind of physical measure to the user, you really want to make sure that you use MeasurementFormatter.

This little gem will take care of automatically converting and displaying physical measures using the correct unit according to the user’s preferences 👌

#04 – ByteCountFormatter

Displaying the size of a file seems easy enough, until you realize all the code that’s needed to choose the appropriate unit depending on the size!

(because no user wants to see a file size of 0.02GB or 1 000 000 KB 🥲)

And that’s precisely when ByteCountFormatter comes to the rescue!

#05 – PersonNameComponentsFormatter

Formatting a person’s name seems pretty straightforward: you write the given name, add a white space, write the family name, and that’s it…

…or is it? Because it turns out that not all regions of the world format a name using this convention!

For instance, Koreans write the family name first and the given name second.

And that’s when PersonNameComponentsFormatter can help take care of this for you!

And that’s it, we’ve completed our tour of Swift’s built-in formatters!

These additional formatters are definitely more niche than the more mainstream DateFormatter or NumberFormatter

…but the day you do need one of them, it will definitely save you a lot of time and effort 😌

Here’s the code if you want to experiment with it:

// #01 - DateComponentsFormatter

import Foundation

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.includesApproximationPhrase = true
formatter.includesTimeRemainingPhrase = true
formatter.allowedUnits = [.minute]
 
// Result: "About 5 minutes remaining"
let outputString = formatter.string(from: 300.0)

// #02 - DateIntervalFormatter

import Foundation

let formatter = DateIntervalFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .none

// Create two dates that are exactly 1 day apart.
let startDate = Date()
let endDate = Date(timeInterval: 86400, since: startDate)

// Result: "03/03/2024 – 04/03/2024”
let outputString = formatter.string(from: startDate, to: endDate))

// #03 - MeasurementFormatter

import Foundation

let formatter = MeasurementFormatter()
formatter.locale = Locale(identifier: "en_US")

let measurement = Measurement(
    value: 1.2,
    unit: UnitLength.kilometers
)

// Result: "0.746 mi"
let outputString = formatter.string(from: measurement)

// #04 - ByteCountFormatter

import Foundation

let formatter = ByteCountFormatter()
formatter.countStyle = .file

// Result: "1 GB"
let outputString = formatter.string(fromByteCount: 1_000_000_000)

// #05 - PersonNameComponentsFormatter

import Foundation

let formatter = PersonNameComponentsFormatter()

var components = PersonNameComponents()
components.givenName = "Vincent"
components.familyName = "Pradeilles"

// Result: "Vincent Pradeilles"
let firstOutputString = formatter.string(from: components)

formatter.locale = Locale(identifier: "ko_KR")

// Result: "Pradeilles Vincent"
let secondOutputString = formatter.string(from: components)
Previous
Previous

How to test Push Notifications in the simulator

Next
Next

Using a Swift macro to generate an EnvironmentKey