My 10 Most Used Extensions for Every iOS Project in Swift

Every time I start a new iOS project I see myself ending up needing the same extra functionalities from the standard Apple’s APIs and digging into previous projects looking how I did solve that particular need. There are already a bunch of nice functions inside standard data types, like String.contains or Array.sort, but for getting the size of a view, there’s no View.width, there’s where Extensions come to the rescue.

What are Extensions?

In Swift, there is this neat feature called Extensions where you can add extra features to the original common classes, identical to categories in Objective C, that is available everywhere in the project. This is only one use case fot Extensions, you can do a lot more with them!

Since Swift first came out and it became open source, a lot of the first extensions that people built became so popular and high in demand that they ended up being added to the shipped API’s.

So what are your most used ones?

Well, here are my 10 most used extensions.

Number 1: Create A Color With A Hex Code

This is a must on every project, being able to create a UIColor with a HEX code, right? Yeah.

Number 2: Transform A Date Into A String

This is also a must on every project, it can get pretty ugly when dealing with dates and strings.

Number 3: Get The Appdelegate Instance

Yap, you’ll end up needing this much more often than you think.

Number 4: Show A Simple Alert Message From Any ViewController Or Object

This should be by default available. Simple and very useful. Notice the fact that it supports sending a notification when the app is in background. Nice!

Number 5: Get The Next Available Parent Viewcontroller From An Object

In the above code you can see this tip number 5, too. Whenever you’re in an object that was originated by a view controller, you’ll sometimes need to grab that view controller instance from the object itself. Be careful with threads and UI changes.

Number 6: Quickly Instantiate An UIView From A Nib / Xib File

Whenever you design your custom views in a separate Nib / Xib file, you’ll want to instantiate it as a UIView somewhere in the code. This comes in very handy.

Number 7: Quickly Create A Snapshot Of A UIViewController As A UIImage

If you’re ever going to deal with animations (eventually you will), taking a snapshot of an UIView is something pretty common. Check out the code above.

Number 8: Missing String Functions That Everyone Should Have In A Simplified Way

Put this next extension in every single project. They are going to simplify things a lot, trust me. Take a minute to look into each one.

Number 9: Remove An Object From An Array

How come this isn’t standard? Do you have an idea? Leave a comment!

Number 10: Represet An Uiimage With A String And Vice Versa

I deal a lot with web services, API’s, so this last extension comes in handy when you want to represent a binary piece of file in a friendly string sequence. Remember: the storage needed to represent a binary file in base64 is at least 30% bigger. To me it’s a comfortable tradeoff.

Did you like it?

How neat is this? The most creative part of a developer gets woken up, and you really begin taking advantage of the great architecture Swift is built on. If you ever though you had great ideas for Extensions, well, here you go, it is probably here! If not, contribute!


So, there’s no right or wrong Extension, there’s no need to import a bunch of them just for the if-ill-need-this-i-have-it situations, you can add them as you go along with your project. The best way to deal with this, and the way I do it, is to think of it as an awesome API you’re capable of developing in no time that fits the exact need of that particular project. Possibilites are endless!

Hope you enjoyed the article. Please share it and comment! I’d love to know what you thought of it and if you have something else to add to this subject.

(Visited 323 times, 1 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *