LSExtensions Usage: Int+

Lee young-jun
2 min readNov 10, 2023

--

When I was iOS Developer at Home, I created a lot of iOS Apps.
And I enjoyed using Swift Extension.

Therefore I made many Extensions for various types whenever I need it.

To share the extensions between my app projects. I decided to archive them into CocoaPods(LSExtensions) and I am using it still now.

However I didn’t provides any Readme and usages.

In this article I will show you usage of my Int extension. And I will post usages for all other extensions one by one.

roman

Sometime I need to create list like this. So I created this.

I. …
II. …
III. …

This extension get property will make Int to Roman numerals.
Unfortunately 13 will be I, because this supports numbers less than 12.

0.roman // I
...
12.roman // XII

lowerAlpha

This extension get property will make a lowercased alphabet sequenced by Int.

1.lowerAlpha // a
...
26.lowerAlpha // z

upperAlpha

This extension get property is the uppercased version of lowerAlpha.

1.lowerAlpha // A
...
26.lowerAlpha // Z

init(alpha: String)

This extension constructor restores the alphabet to Int.
This was useful when I was parsing documents.

1.lowerAlpha // A
...
26.lowerAlpha // Z

stringForDistance(_ : Int = 2)

This extension method makes a distance string from Int.
Only it will supports meters and kilometers.

999.stringForDistance() // 999 m
1000.stringForDistance() // 1 km
1253.stringForDistance() // 1.25 km
1200.stringForDistance() // 1.2 km

stringByComma

This extension property make a string separated by ,(comma) like currency.

1000.stringByComma // 1,000
1000000 // 1,000,000

currencyStringByKor

This extension property make a string to present a Korean currency by Hangul. This will support until trillions(경). Maybe you can see this string in the Korean bank.

1.currencyStringByKor // 일 원
11.currencyStringByKor // 십일 원
111.currencyStringByKor // 백십일 원
...

Int.limit(_: Int, max: Int)

This is not extension method. However attached to Int like static class method.

Int.limit(11, max: 10) // 10
Int.limit(9, max: 10) // 9

If you curious how I implemented these extensions, see Int+.swift.

Use this extensions by install my library.

pod ‘LSExtensions’

Feel free to copy or modify it.
If you want to make your own library, check this article.

Please give me 👏 if this post is helpful to you.
There are more posts about iOS.

Please visit my Linked-In profile if you want to know more about me.

References

--

--

No responses yet