String Format Specifiers

We’ve all grown to love the string format specifiers doc from Apple and because it was baked into NSString, it was super easy to use in Objective-C. Here’s something you might do:

NSLog(@"The current time is %02d:%02d", 10, 4);

While standard string interpolation is useful in Swift, all the power of format specifiers aren’t available using the interpolation syntax. Luckily the format specifiers have been added into the String type during beta. While we can’t do this right from the Swift String type, we can easily use NSString to accomplish our goals.

let timeString = String(format: "The current time is %02d:%02d", 10, 4)

And since NSString and String are interchangeable in Swift, you can easily use the one formatter or another and pass the results right back to Objective-C or Swift.