Member-only story

Breaking Down String Format Specifiers in Swift

Format it well!

Steven Curtis
4 min readJan 10, 2023
Photo by Kira auf der Heide on Unsplash

Before we start

Difficulty: Beginner | Easy | Normal | Challenging

This article has been developed using Xcode 12.2, and Swift 5.3

Prerequisites:

You will be expected to be able to run and use Swift’s Playgrounds

Keywords and Terminology:

String Format Specifiers: This article summarises the format specifiers supported by string formatting methods and functions.

Apple already have a Strings Programming guide for String Format Specifiers, but how are these actually used? In steps this guide

Common formatting

%d, %D

$d and %D represent a signed 32-bit Integer, that is an Int.

In the most basic case we can use the initializer for String to format the Integer

Interpolate with %d

let basicNumber: Int = 42
print(String(format: "%d", basicNumber)) // 42
print(String(format: "Basic Number: %d", basicNumber)) // Basic Number: 42

But we can do more with this initializer.

--

--

No responses yet