Formatting numbers, currencies, distances, colors and addresses in your iOS APP

Introducing Format , a Swift Formatter Kit

I recently made the switch to coding almost exculsively in Swift and I can strongly recommend it. I love writing in Swift but one of the recurring frustrations is the lack of a direct Swift String equivalent to NSString’s precision string format specifiers (e.g. %.02f).

There are many ways to solve this but I wanted to create one that was Swift-y, graceful, responded to a user’s locale (or a custom locale chosen by the developer) and didn’t even require adjusting the type specifier based on the number type.

… and so Format was born. This is how you print Pi with two decimal digits using Format:

print(M_PI.format(Decimals.Two)

Hello Format

Format’s number formatter can be applied directly to any number type and is built on top of NSNumberFormatter but with a nicer and easier to use syntax. It also takes the locale into account which means that for an American user the above line prints 3.14 whereas for a French user it prints 3,14.

I liked this solution so much that I added support for some of the other number formatters provided by iOS:

Decimals.Three // 10.123
Currency.USD // $10.12
General.Ordinal // 10th
General.SpellOut // ten point one two three
General.Distance // 30 feet

As the framework grew into a Swift formatting kit, I was reminded of Matt Thompson’s awesome ObjC FormatterKit and thought it’ll be cool to be able to format other objects too.

Format can now format addresses (there’s even a nice extension on CLPlacemark) and transform hex colors from the web to UIColors you can work with. More formatters will be added soon.

Check it out on:

github.com/marmelroy/Format