5 Useful Swift Extensions to Use in Your iOS App

Make your development process a little bit faster

Zafar Ivaev
Mar 23 · 3 min read
Photo by Dmitry Chernyshov on Unsplash

Today we will quickly explore some great Swift extensions that you can literally copy and paste into your app, and use to suit your needs.

The source code of all extensions is available at the bottom of the article.


UIColor

Here is how we can use it (the code was run in an Xcode Playground):

We can play around with different hex colors using the great website https://flatuicolors.com/.


UISearchBar

print(searchBar.searchTextField)

However, if our users have lower iOS versions, the app will crash. We may obtain the text field using the following code:

It wouldn’t be very convenient if we had to write tedious if else statements every time we need to access a text field, so we have the following great extension to handle this:

Now we can access the text field without worrying about the specific iOS version because everything is done behind the scenes:


UIImage

This extension allows us to create images with a UIColor:


Numeric

Let’s take a look at how we use it:


UITableViewCell and UICollectionViewCell

We can see that we need to create a property cellId and then pass it wherever it is needed. But how could make this code more succinct? Consider this protocol and extensions conforming to it:

Now we are able to access a reuseIdentifier directly from the UITableViewCell or UICollectionViewCell subclass:


Resources


Better Programming

Advice for programmers.

Thanks to Zack Shapiro

Zafar Ivaev

Written by

👨🏻‍💻 iOS & Flutter Developer

Better Programming

Advice for programmers.

More From Medium

More from Better Programming

More from Better Programming

More from Better Programming

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade