PermissionScope

Need expert help for your Objective-C or Swift Project?

👋 I currently have freelance availability to work on your project. Expert UI development, code pairing, tricky refactoring and more: Email me to get started

String Format Specifiers

Ease of use first, fancy formatting second

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.


Questions or comments? Find us on twitter or submit an issue on github

You'll get 1-2 emails per month