How to Format Data, and improve UX in Swift? - Part Two

Note: If you are a beginner and you find it difficult to understand things written in this article. Feel free to ask questions on my Reddit page or Twitter.

If you haven’t read Part One of Data Formatting in Swift and Improving UX, click here.

Remember, you can do it. 😉


I hope you enjoyed formatting Lists & Dates. It’s time for Numbers & Names.

Number Formatting

What do we do with Number Formatting? What kind of Number Formatting are available for us?

Well, We have a lot of formatters predefined by Swift. We’ll be exploring Currency Formatting for now. You can always play with the code in Playground, do modifications as per your satisfaction, but don’t forget to update about it in comments.

let number: NSNumber = 110.59

let currencyFormatter = NumberFormatter()
currencyFormatter.numberStyle = .currencyISOCode
currencyFormatter.currencyCode = "INR"

//currencyFormatter.numberStyle = .currency
//currencyFormatter.currencySymbol = "₹"

//currencyFormatter.locale = Locale(identifier: "es_ES") // Or use: Locale.current

let amount = currencyFormatter.string(from: number) // Output: INR 110.59

It’s so easy to format currency with localization. I have commented some lines of code for you guys to experiment with the code in your Playground.

Localization allows us to be able to show data without if-else statements. Just use Locale.current, and you are good to go.

Initially, We declared a variable of type NSNumber, and you need to make sure that your number is of type NSNumber, cause NumberFormatter doesn’t support any other types.

Next, we initialized NumberFormatter and modified it’s properties to show the number with ISOCode of currency INR.

Next, there are some comments, which are solely for your practice.

Finally, we retrieve the string with the help of our formatter. The final output is INR 110.59.

Remember, there are many other types of formatting you can do. Below, in the image, you can see all kinds of styling available in NumberFormatter.

Image Source: developer.apple.com

Image Source: developer.apple.com

Person Name Formatter

Name is one of the essential things in almost all Applications. But do we ever care about how we are representing it?

Some of us do, and that is why you are here reading this article.

Let’s look into what goes into the code.

let name = "Karan Pal"

let formatter = PersonNameComponentsFormatter()

let components = formatter.personNameComponents(from: name) // Returns PersonNameComponents object.

components?.givenName   // Output: "Karan"
components?.middleName  // Output: nil
components?.familyName  // Output: "Pal"

formatter.style = .abbreviated
formatter.string(from: components!) // Output: "KP"

Our first line starts with declaring my Name. Next, we initialized the formatter, and then with the help of formatter retrieved an object of type PersonNameComponents.

PersonNameComponents help us separate parts of the name, and with that, we can get/set different parts of the name, with the help of dot-notation. As you can see in the code above, I have left the comments next to the statements for better understanding.

Below are the styles shown in the image, available as of now.

Image Source: developer.apple.com

Image Source: developer.apple.com

Always represent your data in the best way possible; taking the extra mile is better than losing a user.

Feel free to run this code in your playground, and make modifications.

Experimenting is a way of Coding.
— Swift Pal

WWDC 2020

WWDC 2020 is near. Don’t miss out on the latest updates. Subscribe to receive updates.

If you have any suggestions or questions, Feel free to connect me on Twitter. 😉

How to use GCD (Grand Central Dispatch) in Swift? - Synchronous & Asynchronous Programming Tutorial

How to Format Data, and improve UX in Swift? - Part One