Yet Another Useful Set of Swift Extensions

Moussa Hellal
3 min readJan 7, 2021

--

Hello, let’s start the year 2021 with a lightweight article for you Swift developers around the globe. Together we will check out some useful extensions you can use daily.

By the way, every extension has a usage example. Great news ha?! Yeah, I know.

Drum playing…

  • Starting with String:
  1. The strikeThrough extension allows us to strike through a given string to be able to use it in our views, like the below example in UILabel:

Our UILabel Output:

2. The toDouble extension converts a given string to Double. Here is an example:

Output:

  • Second, let’s move to Array:

Let’s shuffle an array:

Output: Shuffled enough right?

  • Third, let’s check Sequence:

Slicing an array into groups can never be easier. Here is a sample:

Output: As you can see here, a very simple example that is grouped by the repeated value. You can play with it and group an array by Date for example. Go ahead try it on your own.

  • Now with Double:

Rounding a value with given places:

Output: rounded with 3 as we passed in our argument.

  • Finally, let’s with Date:

Converting a Date into a string:
- We can use the toDateString() to convert directly a given into a simple date string.
- toTimeString() converts a given Date value into time string.
-
Finally, we see the toGiven(DateTimeFormat format: string) this is very useful when you need to pass in a different date format every time.

Output:

Bonus: Here are more useful Date Formats you can use through our last extension toGiven(DateTimeFormat format: string)

example: toGiven(DateTimeFormat format: “MMM d, h:mm a”) will give you: Sep 12, 2:11 PM

by BatyrCan in https://stackoverflow.com/questions/35700281/date-format-in-swift

Hope you have learned something new and useful today.

By the way here is my Twitter for more:

https://twitter.com/HellalMoussa

Thank you!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response